From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 1D0BEBB81 for ; Mon, 6 Mar 2006 20:25:27 +0100 (CET) Received: from fork.recoil.org (fork.recoil.org [194.70.3.132]) by concorde.inria.fr (8.13.0/8.13.0) with SMTP id k26JPQX4001170 for ; Mon, 6 Mar 2006 20:25:26 +0100 Received: (qmail 13638 invoked by uid 10000); 6 Mar 2006 19:25:26 -0000 Date: Mon, 6 Mar 2006 19:25:26 +0000 From: Anil Madhavapeddy To: Michael Wohlwend Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] recursive or loop Message-ID: <20060306192526.GA29897@fork.recoil.org> References: <200603061715.k26HF32w015203@nez-perce.inria.fr> <200603061925.35321.micha-1@fantasymail.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200603061925.35321.micha-1@fantasymail.de> User-Agent: Mutt/1.5.11 X-Miltered: at concorde with ID 440C8CA6.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; anil:01 anil:01 recursive:01 conceptually:01 threading:01 camlp:01 stack:01 recursion:01 sml:01 ocaml:01 ocaml:01 stack:01 recursive:01 2006:98 2006:98 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 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