<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/11/32f0890d60db507d62ab7a73dcdc1e62"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-11-17T20:44:47"
  subject="Re: [Caml-list] Closure &amp; Ref"
  prev="2003/11/d708e5671623d9731257696b418a4325"
  next="2003/11/c62502b67c01192d18d301d00e9d2b31"
  prev-in-thread="2003/11/398e682363175b0a71bf952390df0eae"
  next-in-thread="2003/11/46312cc55957d6ec48f8b0439fb04d84"
  prev-thread="2003/11/496b39537f731229ef7c3864e45156c0"
  next-thread="2003/11/4d893cc52b22fe3904f8f579fcf78b2d"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Closure &amp; Ref">
<msg 
  url="2003/11/398e682363175b0a71bf952390df0eae"
  from="chris.danx &lt;chris.danx@n...&gt;"
  author="chris.danx"
  date="2003-11-17T19:32:44"
  subject="[Caml-list] Closure &amp; Ref">
<msg 
  url="2003/11/32f0890d60db507d62ab7a73dcdc1e62"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-11-17T20:44:47"
  subject="Re: [Caml-list] Closure &amp; Ref">
<msg 
  url="2003/11/46312cc55957d6ec48f8b0439fb04d84"
  from="chris.danx &lt;chris.danx@n...&gt;"
  author="chris.danx"
  date="2003-11-17T21:30:04"
  subject="Re: [Caml-list] Closure &amp; Ref">
</msg>
</msg>
<msg 
  url="2003/11/c62502b67c01192d18d301d00e9d2b31"
  from="Dustin Sallings &lt;dustin@s...&gt;"
  author="Dustin Sallings"
  date="2003-11-17T21:02:39"
  subject="Re: [Caml-list] Closure &amp; Ref">
<msg 
  url="2003/11/5f963c7ed269cdd4ed0e0979ee211090"
  from="Dustin Sallings &lt;dustin@s...&gt;"
  author="Dustin Sallings"
  date="2003-11-17T21:48:23"
  subject="Re: [Caml-list] Closure &amp; Ref">
</msg>
</msg>
</msg>
</thread>

<contents>
On Mon, 17 Nov 2003, chris.danx wrote:

&gt; Hi,
&gt; 
&gt; I was toying with ocaml just now and have successfully written a 
&gt; function that takes and int that produces a function that takes an int 
&gt; to add to the original.
&gt; 
&gt; let prodAdd x =
&gt;     let value = ref x in
&gt;        fun y -&gt; !value + y;;

Since you're not setting the reference, why have one?  Instead, try:

let prodAdd x = fun y -&gt; x + y

But we can do it simpler than that:

let prodAdd x y = x + y

And use partial function application.  (prodAdd 4) returns a function 
which adds 4 to whatever int parameter passed to it.

&gt; 
&gt; Now I want to do a function that takes a ref to a list and returns a 
&gt; function that adds items to the list and produce a function that returns 
&gt; another that returns the list.  How do I do that?
&gt; 
&gt; let prod_list_acc a =
&gt;    fun x -&gt; a := x :: !a; true;;
&gt; 
&gt; let return_acc a =
&gt;    fun () -&gt; !a;;
&gt; 
&gt; but that gives a "unit -&gt; int list" =.  How do you get a copy of the 
&gt; list values?

Now you're setting the reference.  But prod_list_acc and return_acc need 
to share the reference.

Now, I for one, hate globals.  This is the result of programming for years 
in C.  What I would do is write a function which returns a tuple of two 
functions, and accumlator function and a current list function, like:

let make_listacc () =
	let r: int list ref = ref [] in
	let acc x = r := x :: !r
	and lst () = !r
	in acc, lst
;;

This allows you to have multiple different lists being constructed 
independently.  Note, the above code is actually more generic than it 
looks- I had to add an explicit type statement to make it "come out 
correct".  Without the explicit type information:

let make_listacc () =
        let r = ref [] in
        let acc x = r := x :: !r
        and lst () = !r
        in acc, lst
;;

The function is both clearer than the original, and creates lists of any 
type, not just ints.  If you hear me bitching about C++ and Java making 
generics "special and extraordinary", this is a classic example of what 
I'm kvetching about.

You'd use make_listacc like:

	let my_acc, my_list = make_listacc() in
	my_acc 3;
	my_acc 4;
	my_acc 5;
	my_list ()
	;;

The above code returns [5;4;3] (note, the list is built backward!).

The most common way to access members of a list is to use what is called 
"list comprehensions".  Don't let the names fool you- these are just 
functions that walk the list and do something on every element of the 
list.  Look at List.iter and List.fold_left.  So let's say I want to take 
a list of ints and sum them.  I could simply do:
	List.fold_left (fun x y -&gt; x + y) 0 lst

Note that + is just a function, so I could just as easily have done:
	List.fold_left (+) 0 lst
to do the same thing.

The second most common way to access members of a list is to write a short 
recursive function.  Say I wanted to know if the list contained a given 
number.  I might write:
	let rec has_num x = function
		| [] -&gt; false
		| h :: t -&gt; if (h == x) then true else has_num x t
	;;

If you want to access specific members of a list (other than the head), I
wouldn't recommend using a list, but instead some other datastructure (an
array or hash table being the obvious choices).


-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

</contents>

</message>

