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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 9B334BC37 for ; Tue, 9 Feb 2010 23:56:28 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtoBAM92cUvUnwdklGdsb2JhbACDCZddFQEBAQEJCwgJEwQfrT6QP4EvgkpbBA X-IronPort-AV: E=Sophos;i="4.49,439,1262559600"; d="scan'208";a="56565358" Received: from relay.pcl-ipout02.plus.net ([212.159.7.100]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 09 Feb 2010 23:56:27 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av0EAFJ3cUtUXebz/2dsb2JhbACDCZdddK1GkD2BL4JKWwQ Received: from relay02.plus.net ([84.93.230.243]) by relay.pcl-ipout02.plus.net with ESMTP; 09 Feb 2010 22:56:27 +0000 Received: from [87.112.77.220] (helo=leper.local) by relay02.plus.net with esmtp (Exim) id 1Nez0M-0001CB-N2; Tue, 09 Feb 2010 22:56:26 +0000 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: saptarshi.guha@gmail.com Subject: Re: [Caml-list] The need to specify 'rec' in a recursive function defintion Date: Wed, 10 Feb 2010 00:12:03 +0000 User-Agent: KMail/1.9.9 Cc: caml-list@yquem.inria.fr References: <1e7471d51002091250of7a686fq537a03c9401c868f@mail.gmail.com> <201002092333.39698.jon@ffconsultancy.com> <1e7471d51002091431m6451a2b5p35718c57920e9071@mail.gmail.com> In-Reply-To: <1e7471d51002091431m6451a2b5p35718c57920e9071@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201002100012.03809.jon@ffconsultancy.com> X-Plusnet-Relay: 2ff41a780e475f4a19dd70842f58c176 X-Spam: no; 0.00; recursive:01 defintion:01 recursive:01 bindings:01 vars:01 vars:01 expr:01 expr:01 ocaml:01 ocaml:01 recursion:01 frog:98 idiom:01 wrote:01 rec:01 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