<?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/c62502b67c01192d18d301d00e9d2b31"
  from="Dustin Sallings &lt;dustin@s...&gt;"
  author="Dustin Sallings"
  date="2003-11-17T21:02:39"
  subject="Re: [Caml-list] Closure &amp; Ref"
  prev="2003/11/32f0890d60db507d62ab7a73dcdc1e62"
  next="2003/11/41e94f145d604f09aacb6c4947bc279a"
  prev-in-thread="2003/11/46312cc55957d6ec48f8b0439fb04d84"
  next-in-thread="2003/11/5f963c7ed269cdd4ed0e0979ee211090"
  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 Nov 17, 2003, at 11:37, chris.danx wrote:

&gt; let prodAdd x =
&gt;    let value = ref x in
&gt;       fun y -&gt; !value + y;;

	This is very similar to

let prodAdd x y = x + y;;

	The only difference is that I'm making a reference with the value of x.

# let prodAdd x = let value = ref x in fun y -&gt; !value + y;;
val prodAdd : int -&gt; int -&gt; int = &lt;fun&gt;
# let prodAdd2 x y = x + y;;
val prodAdd2 : int -&gt; int -&gt; int = &lt;fun&gt;
# prodAdd 1 2;;
- : int = 3
# prodAdd2 1 2;;
- : int = 3
# prodAdd 1;;
- : int -&gt; int = &lt;fun&gt;
# prodAdd2 1;;
- : int -&gt; int = &lt;fun&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 
&gt; returns 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?
&gt;
&gt;
&gt; This might seem like a crazy thing to do but I am toying with an idea 
&gt; relating to a paper I read on traits.  Instead of having classes at 
&gt; all you can just have traits, closures and mutable values.

	That's not a very functional style.  It does seem to work for me with 
the following syntax, though:

  # let list_acc a x = a := x :: !a; ();;
val list_acc : 'a list ref -&gt; 'a -&gt; unit = &lt;fun&gt;
# let return_list a = !a;;
val return_list : 'a ref -&gt; 'a = &lt;fun&gt;
# return_list a;;
- : int list = [1; 2; 3; 4; 5]
# list_acc a 6;;
- : unit = ()
# return_list a;;
- : int list = [6; 1; 2; 3; 4; 5]


-- 
Dustin Sallings

-------------------
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>

