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: Pierre Weis <pierre.weis@i...>
Subject: Re: [Caml-list] why the "rec" in "let rec"?
[...]
> To summarize, the difference between "let" and "let rec" is that they
> are based on theories with different strengths, and the language
> designers don't want to unify such constructs (IMHO, a good attitude).
> 
> Gerd
> -- 
> ------------------------------------------------------------
> Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
> gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
> ------------------------------------------------------------

An important additional advantage of identifier rebinding
is to avoid confusion between the new and old value: the rebinding makes
it explicit that the old value is now irrelevant and the language
scoping rules will then ensure this property.

For example, consider the following (pseudo)-code snippet where we
carefully avoid rebinding of i, using a new name j instead:

 ... i ...
 let j = i + 1 in
 ... j ...         (* 3 *)

In these circumstances, the programmer can confuse i and j, using i
instead of j in part (* 3 *). Since, unfortunately, i and j have the
same type, the compiler can not detect this error. By contrast,
rebinding i elegantly avoids this problem:

 ... i ...
 let i = i + 1 in  (* 2 *)
 ... i ...         (* 3 *)

after (* 2 *) the old value of i is lost (no more accessible) and the
code is thus clearer and more robust in my opinion.

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
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