caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: saptarshi.guha@gmail.com
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] The need to specify 'rec' in a recursive function  defintion
Date: Wed, 10 Feb 2010 00:12:03 +0000	[thread overview]
Message-ID: <201002100012.03809.jon@ffconsultancy.com> (raw)
In-Reply-To: <1e7471d51002091431m6451a2b5p35718c57920e9071@mail.gmail.com>

On Tuesday 09 February 2010 22:31:49 Saptarshi Guha wrote:
> Yes, I see  that f isn't recursive, because it simplifies down to
> 2*(x+1) but for a reader(at least myself) it can be bit tricky to
> consume.

But for a writer it can be useful to produce. For example, imagine a function 
in an interpreter that evaluates an expression to a value in the context of a 
set of variable bindings:

  let rec eval vars = function
    ...
    | Add(f, g) -> eval vars f + eval vars g
    | Mul(f, g) -> eval vars f * eval vars g
    ...

Such functions make a *lot* of recursive calls.

Now imagine you want to inject some code around every external call into 
that "expr" function but not recursive calls, e.g. logging exceptional 
returns. Do you want to change the name of that "expr" function not only in 
its definition but at every recursive call in its body? No.

Fortunately, in OCaml you can just append a definition to supercede "expr" 
without touching the original definition at all:

  let eval vars expr =
    ...
    let result = eval vars expr in
    ...
    result

This is a useful and common OCaml idiom.

> My experience of reading the /definition/ of a function which 
> includes a call to itself is that it is recursive.

Definitions are often superceded in ML.

> On the stackoverflow post, you mentioned that the British ML branch forces
> different names (since recursion is by default), and though it does pollute
> the namespace, personally I find it easier to read.

You can do that in OCaml as well, if you choose.

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


  reply	other threads:[~2010-02-09 22:56 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-09 20:50 Saptarshi Guha
2010-02-09 21:55 ` [Caml-list] " Guillaume Yziquel
2010-02-09 22:14   ` Saptarshi Guha
2010-02-09 22:01 ` Gerd Stolpmann
2010-02-09 21:58   ` Guillaume Yziquel
2010-02-09 22:34     ` Gerd Stolpmann
2010-02-10  0:07       ` Guillaume Yziquel
2010-02-10  3:10         ` Alain Frisch
2010-02-09 22:16   ` Saptarshi Guha
2010-02-09 23:29   ` Jon Harrop
2010-02-10 10:15     ` rossberg
2010-02-10  7:19   ` Andrej Bauer
2010-02-10  9:36     ` Francois Maurel
2010-02-10 10:12     ` rossberg
2010-02-09 23:33 ` Jon Harrop
2010-02-09 22:31   ` Saptarshi Guha
2010-02-10  0:12     ` Jon Harrop [this message]
2010-02-10 22:01 ` Stefan Monnier
2010-02-10 22:25   ` [Caml-list] " Till Varoquaux
2010-02-11  1:48     ` Jon Harrop
2010-02-15 15:46     ` Stefan Monnier
2010-02-15 17:33       ` [Caml-list] " Jon Harrop
2010-02-15 20:36         ` Stefan Monnier
2010-02-16 14:42           ` Stefan Monnier
2010-02-16 16:21             ` [Caml-list] " Ashish Agarwal

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=201002100012.03809.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=saptarshi.guha@gmail.com \
    /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).