From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA27617; Mon, 17 Nov 2003 22:02:39 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA27251 for ; Mon, 17 Nov 2003 22:02:38 +0100 (MET) Received: from bob.west.spy.net (mail.west.spy.net [66.149.231.226]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id hAHL2b122317 for ; Mon, 17 Nov 2003 22:02:37 +0100 (MET) Received: from [10.9.254.240] (sjc-dist3-e3.2wire.com [63.203.253.2]) by bob.west.spy.net (Postfix) with ESMTP id 262616350; Mon, 17 Nov 2003 13:02:32 -0800 (PST) In-Reply-To: <3FB92396.4060908@ntlworld.com> References: <3FB92396.4060908@ntlworld.com> Mime-Version: 1.0 (Apple Message framework v606) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: <5CBC9D22-1941-11D8-BBC4-000393CFE6B8@spy.net> Content-Transfer-Encoding: 7bit Cc: caml-list@inria.fr From: Dustin Sallings Subject: Re: [Caml-list] Closure & Ref Date: Mon, 17 Nov 2003 13:02:32 -0800 To: "chris.danx" X-Mailer: Apple Mail (2.606) X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 val:01 val:01 toying:01 traits:01 traits:01 closures:01 chris:01 mutable:01 closure:01 int:01 int:01 nov:01 syntax:02 unit:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Nov 17, 2003, at 11:37, chris.danx wrote: > let prodAdd x = > let value = ref x in > fun y -> !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 -> !value + y;; val prodAdd : int -> int -> int = # let prodAdd2 x y = x + y;; val prodAdd2 : int -> int -> int = # prodAdd 1 2;; - : int = 3 # prodAdd2 1 2;; - : int = 3 # prodAdd 1;; - : int -> int = # prodAdd2 1;; - : int -> int = > Now I want to do a function that takes a ref to a list and returns a > function that adds items to the list and produce a function that > returns another that returns the list. How do I do that? > > let prod_list_acc a = > fun x -> a := x :: !a; true;; > > let return_acc a = > fun () -> !a;; > > but that gives a "unit -> int list" =. How do you get a copy of the > list values? > > > This might seem like a crazy thing to do but I am toying with an idea > relating to a paper I read on traits. Instead of having classes at > 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 -> 'a -> unit = # let return_list a = !a;; val return_list : 'a ref -> 'a = # 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