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
Re: [Caml-list] recursive or loop
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-03-06 (19:25)
From: Anil Madhavapeddy <anil@r...>
Subject: Re: [Caml-list] recursive or loop
On Mon, Mar 06, 2006 at 07:25:35PM +0100, Michael Wohlwend wrote:
> On Monday 06 March 2006 18:25, Jonathan Harrop wrote:
> > Write in CPS?
> >
> no experience with that. Doesn't it need support of the language?

It's conceptually pretty simple; every function takes an extra
argument which receives the result instead of explicitly returning
it.  It permits a neat alternative to threading since you can
``suspend'' a computation by not applying it to the next function
in the chain of control, and so write custom thread scheduling
policies without depending on your OS to do the right thing.

In practise, it does need some support from the language to be
useful.  Syntactic help is essential to avoid lots of (fun () ->
()) style closures for every statement; this may be something you
want to play with using camlp4, but is not for the faint of heart.

A more serious problem is that their creation can require the
contents of the stack to be copied if you suspend the continuatation.
This is obviously a bad thing performance-wise if you love your
recursion.  A notable difference between SML/NJ and OCaml is that
the former natively supports continuations; OCaml adopts an abstract
machine which maps more cleanly onto modern hardware but makes the
efficient use of continuations without stack copying difficult.

To answer your original question, I wouldn't get too religious about
recursive vs imperative styles; OCaml lets you choose and mix them,
so pick the one that gets the job done for you and move on to the
finer things in life :-)

Anil Madhavapeddy                   
University of Cambridge