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 CAA31414; Mon, 26 Apr 2004 02:45:32 +0200 (MET DST) 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 CAA31089 for ; Mon, 26 Apr 2004 02:45:31 +0200 (MET DST) Received: from smtp1.adl2.internode.on.net (smtp1.adl2.internode.on.net [203.16.214.181]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i3Q0jRYM004167 for ; Mon, 26 Apr 2004 02:45:29 +0200 Received: from [192.168.1.200] (ppp119-113.lns1.syd2.internode.on.net [150.101.119.113]) by smtp1.adl2.internode.on.net (8.12.9/8.12.9) with ESMTP id i3Q0jKZq039605; Mon, 26 Apr 2004 10:15:21 +0930 (CST) Subject: Re: [Caml-list] help From: skaller Reply-To: skaller@users.sourceforge.net To: mohammad siddiqui Cc: jdh30@cam.ac.uk, caml-list In-Reply-To: References: Content-Type: text/plain Message-Id: <1082940320.9537.476.camel@pelican.wigram> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-4) Date: 26 Apr 2004 10:45:20 +1000 Content-Transfer-Encoding: 7bit X-Miltered: at = by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 sourceforge:01 2004:99 demonstrate:01 wrd:99 wrd:99 endline:01 9660:01 glebe:01 anyhow:01 mutable:01 mutable:01 int:01 nsw:01 snail:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Mon, 2004-04-26 at 02:30, mohammad siddiqui wrote: > John, > > I tried exactly what you have told. Its is fine when we just have one > element in an array of words. If we have more than one, changing the value > of one changes the values of the rest of the elements. I suggest you first try a *purely functional* approach. The reason is: its easier! It will work exactly as you expect, no suprises .. I promise . Get rid of references and mutable fields. Forget efficiency concerns at this time. Here is how to make a doc, containing words, where i have simplified the data structures to demonstrate: type wrd = string type doc = { id:int; words: wrd list } let add_word d w= { d with words = w::d.words } (* NOTE: functional update syntactic sugar saves explicitly copying each field .. unmentioned fields get copied automatically *) let empty = { id=99; words=[] } let d = ref empty ;; List.iter (fun w -> d := add_word !d w) ["Hello";"This";"is";"a";"Document";"of";"words"] ;; List.iter print_endline (!d.words) ;; Note here i AM using a reference to hold the doc, but the technique of adding a word to a doc is purely functional: you get a brand new doc for every word you add. EXERCISE: use a fold to get rid of the 'd' variable, making this little program *purely* functional (up to the printing part anyhow :D QUESTION: how fast is the functional technique compared with using mutable fields or references to modify a data structure in place?? I don't know the answer . but I'm going to GUESS. The functional technique is 10% FASTER! -- John Skaller, mailto:skaller@users.sf.net voice: 061-2-9660-0850, snail: PO BOX 401 Glebe NSW 2037 Australia Checkout the Felix programming language http://felix.sf.net ------------------- 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