English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
The need to specify 'rec' in a recursive function defintion
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-02-09 (22:18)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] The need to specify 'rec' in a recursive function defintion
On Tuesday 09 February 2010 20:50:33 Saptarshi Guha wrote:
> Hello,
>  I was wondering why recursive functions need to be specified with
> "rec". According to Practical Ocaml, to "inform the compiler that the
> function 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"?
> How is the knowledge of a function being rec taken advantage of (in
> ocaml) as opposed to other languages
> (leaving aside tail call optimization).
> Wouldn't one of way of detecting a recursive function would be to see
> if the indeed the function calls itself?

  let f x = x + 1
  let f x = 2 * f x

Is the latter "f" recursive or not?

See my answer to the same question on stack overflow:


Dr Jon Harrop, Flying Frog Consultancy Ltd.