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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 9FC10BC37 for ; Tue, 9 Feb 2010 22:54:43 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnQDAINocUtV2gB5hWdsb2JhbACDCZdxAQEBCgsKBRUjrTqQPoEvgkpbBIla X-IronPort-AV: E=Sophos;i="4.49,438,1262559600"; d="scan'208";a="52235010" Received: from emailfrontal2.citycable.ch ([85.218.0.121]) by mail1-smtp-roc.national.inria.fr with SMTP; 09 Feb 2010 22:54:43 +0100 Received: from [192.168.0.12] (unknown [85.218.92.99]) (Authenticated sender: guillaume.yziquel@citycable.ch) by emailfrontal2.citycable.ch (Postfix) with ESMTPA id 5FB29834377; Tue, 9 Feb 2010 22:54:40 +0100 (CET) Message-ID: <4B71D9BB.3090605@citycable.ch> Date: Tue, 09 Feb 2010 22:55:07 +0100 From: Guillaume Yziquel Reply-To: guillaume.yziquel@citycable.ch User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090707) MIME-Version: 1.0 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 References: <1e7471d51002091250of7a686fq537a03c9401c868f@mail.gmail.com> In-Reply-To: <1e7471d51002091250of7a686fq537a03c9401c868f@mail.gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; guillaume:01 guillaume:01 recursive:01 defintion:01 recursive:01 ocaml:01 compiler:01 compiler:01 ocaml:01 avoided:01 unbound:01 unbound:01 rec:01 rec:01 recursively:01 Saptarshi Guha a =C3=A9crit : > Hello, > I was wondering why recursive functions need to be specified with > "rec". According to Practical Ocaml, to "inform the compiler that the f= unction > exists". But when entering the function definition, can't the compiler = note that > the function is being defined so that when it sees the function calling= itself, > it wont say "Unbound value f"? Essentially, the fact that "rec" means anything is mostly due to the=20 fact that you want to be able to redefine stuff. > let f =3D fun x -> x + 1 >=20 > let f x =3D f (f x) is valid in ocaml, and you're function f is the function that adds 2. whereas > let rec f x =3D f (f x) has a completely different meaning. If you avoided the use of rec, saying "if unbound, assume rec", then the=20 line let f x =3D f (f x) has two entirely different meaning, depending on whether f is defined=20 before or not. That would be quite a chaotic feature. > Wouldn't one of way of detecting a recursive function would be to see > if the indeed the function calls itself? No. Because you never know if you intended to call the function=20 recursively, or if you intended to call an homonymous function you=20 defined before. All the best, --=20 Guillaume Yziquel http://yziquel.homelinux.org/