From: skaller <skaller@users.sourceforge.net>
To: mohammad siddiqui <writetosafi@hotmail.com>
Cc: jdh30@cam.ac.uk, caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] help
Date: 26 Apr 2004 10:45:20 +1000 [thread overview]
Message-ID: <1082940320.9537.476.camel@pelican.wigram> (raw)
In-Reply-To: <BAY7-F107rcR4ZDgC8100004e0f@hotmail.com>
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 <g>.
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
next prev parent reply other threads:[~2004-04-26 0:45 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-04-25 16:30 mohammad siddiqui
2004-04-25 1:53 ` mohammad siddiqui
2004-04-25 12:13 ` Jon Harrop
2004-04-25 12:45 ` Henri DF
2004-04-26 19:13 ` Jon Harrop
2004-04-26 22:38 ` Andrew Lenharth
2004-04-25 16:44 ` [Caml-list] help Brian Hurt
2004-04-25 17:28 ` [Caml-list] help Matt Gushee
2004-04-25 17:06 ` Jon Harrop
2004-04-25 17:39 ` Matt Gushee
2004-04-26 0:45 ` skaller [this message]
-- strict thread matches above, loose matches on Subject: below --
2004-04-23 2:31 mohammad siddiqui
2004-04-23 4:17 ` Jon Harrop
2004-04-23 6:52 ` Jean-Christophe Filliatre
2004-04-23 15:43 ` Brian Hurt
2004-04-22 23:34 mohammad siddiqui
2004-04-22 23:57 ` Karl Zilles
2004-04-16 18:04 mohammad siddiqui
2004-04-16 18:59 ` skaller
2004-04-17 0:34 ` Shawn Wagner
2004-04-08 22:57 mohammad siddiqui
2004-04-09 6:56 ` Basile STARYNKEVITCH
2004-04-09 9:04 ` Richard Jones
2002-11-05 12:22 ÀîÒÇ
2002-11-05 13:39 ` Alessandro Baretta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1082940320.9537.476.camel@pelican.wigram \
--to=skaller@users.sourceforge.net \
--cc=caml-list@inria.fr \
--cc=jdh30@cam.ac.uk \
--cc=writetosafi@hotmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox