caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Anil Madhavapeddy <anil@recoil.org>
To: Michael Wohlwend <micha-1@fantasymail.de>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] recursive or loop
Date: Mon, 6 Mar 2006 19:25:26 +0000	[thread overview]
Message-ID: <20060306192526.GA29897@fork.recoil.org> (raw)
In-Reply-To: <200603061925.35321.micha-1@fantasymail.de>

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                             http://anil.recoil.org
University of Cambridge                      http://www.cl.cam.ac.uk


  parent reply	other threads:[~2006-03-06 19:25 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-06 17:25 Jonathan Harrop
2006-03-06 18:25 ` Michael Wohlwend
2006-03-06 18:31   ` Thomas Fischbacher
2006-03-08 19:07     ` Matthew O'Connor
2006-03-08 21:56       ` Thomas Fischbacher
2006-03-08 22:10         ` Till Varoquaux
2006-03-08 22:13           ` Thomas Fischbacher
2006-03-08 23:28             ` Jon Harrop
2006-03-06 19:25   ` Anil Madhavapeddy [this message]
2006-03-06 21:22     ` Michael Wohlwend
2006-03-07 16:02       ` Alan Falloon
2006-03-08  7:53         ` Michael Wohlwend
  -- strict thread matches above, loose matches on Subject: below --
2006-03-06 13:33 Jonathan Harrop
2006-03-06 14:15 ` Thomas Fischbacher
2006-03-06 16:42   ` Michael Wohlwend

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=20060306192526.GA29897@fork.recoil.org \
    --to=anil@recoil.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=micha-1@fantasymail.de \
    /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).