From: Brian Hurt <bhurt@spnz.org>
To: "chris.danx" <chris.danx@ntlworld.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Closure & Ref
Date: Mon, 17 Nov 2003 15:43:59 -0600 (CST) [thread overview]
Message-ID: <Pine.LNX.4.44.0311171522360.5009-100000@localhost.localdomain> (raw)
In-Reply-To: <3FB92396.4060908@ntlworld.com>
On Mon, 17 Nov 2003, chris.danx wrote:
> Hi,
>
> I was toying with ocaml just now and have successfully written a
> function that takes and int that produces a function that takes an int
> to add to the original.
>
> let prodAdd x =
> let value = ref x in
> fun y -> !value + y;;
Since you're not setting the reference, why have one? Instead, try:
let prodAdd x = fun y -> 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.
>
> 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?
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 -> 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
| [] -> false
| h :: t -> 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
next prev parent reply other threads:[~2003-11-17 20:44 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-11-17 19:37 chris.danx
2003-11-17 21:02 ` Dustin Sallings
2003-11-17 21:48 ` Dustin Sallings
2003-11-17 21:43 ` Brian Hurt [this message]
2003-11-17 21:35 ` chris.danx
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=Pine.LNX.4.44.0311171522360.5009-100000@localhost.localdomain \
--to=bhurt@spnz.org \
--cc=caml-list@inria.fr \
--cc=chris.danx@ntlworld.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;
as well as URLs for NNTP newsgroup(s).