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] two unrelated questions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Max Skaller <skaller@o...>
Subject: Re: [Caml-list] two unrelated questions
Mark Seaborn wrote:

> Another way of doing it is to CPS-convert the fold function to get
> something like:
> let rec foldc kons knil l = match l with
>   | [] -> knil
>   | x::xs -> kons x knil (fun rest -> foldc kons rest xs)

> This is more powerful than fold, but also harder to understand,

	You're not kidding, on both counts :-)

	It's also 'ugly', because it only works for lists,
but this is only because you hard coded the destructor.
Passing it as an argument:

  let rec foldc remove kons knil l = match remove l with
	| (_,None) -> knil
	| (xs,Some x) -> kons x knil (fun rest -> foldc remove kons rest xs)

  let foldc_list = foldc (fun l -> match l with 
		| [] -> l,None
		| x::xs -> xs,Some x)

It's a pity there is no such destructor for a Hashtbl. 
[All C++ STL data structures have read iterators, which 
amount to destructors]

John (Max) Skaller,
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
checkout Vyper
download Interscript
To unsubscribe, mail  Archives: