Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Closure & Ref
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-11-17 (20:44)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] Closure & Ref
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 

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: