Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] why the "rec" in "let rec"?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris Uzdavinis <chris@a...>
Subject: Re: [Caml-list] why the "rec" in "let rec"?
Garry Hodgson <garry@sage.att.com> writes:

> something i was always curious about: why do you need to specify the
> "rec" in a "let rec" function definition?  as opposed to, say,
> having the compiler figure out when a function is recursive?
> 
> is it a compiler/grammar optimization?  or to help the user, forcing
> them to be precise with recursion?  or required by the type system?

It affects the name lookup rules.  For example:

  let f x = x
  let f x = f x

The 2nd definition for function f is not an infinite loop.  It calls
the previously defined version of f, and is thus a more expensive
identity function.  However:

  let f x = x
  let rec f x = f x

Now the first function is not used by the second (which, due to the
"rec" having been added, is now an infinite loop calling itself.)

> do other ML's do it this way?

Yes.

-- 
Chris

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners