From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA14166; Wed, 7 May 2003 16:45:51 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA14124 for ; Wed, 7 May 2003 16:45:50 +0200 (MET DST) Received: from h00045a4799d6.ne.client2.attbi.com (h00045a4799d6.ne.client2.attbi.com [65.96.179.155]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h47EjnH12453 for ; Wed, 7 May 2003 16:45:50 +0200 (MET DST) Received: by h00045a4799d6.ne.client2.attbi.com (Postfix, from userid 500) id 2C41D2BBAB; Wed, 7 May 2003 10:50:11 -0400 (EDT) From: Neel Krishnaswami MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <16057.7459.42552.45637@h00045a4799d6.ne.client2.attbi.com> Date: Wed, 7 May 2003 10:50:11 -0400 To: "caml-list@inria.fr" Subject: Re: [Caml-list] why the "rec" in "let rec"? In-Reply-To: <2003050710041052316284@kestrel.sage.att.com> References: <2003050710041052316284@kestrel.sage.att.com> X-Mailer: VM 7.04 under 21.4 (patch 8) "Honest Recruiter" XEmacs Lucid X-Spam: no; 0.00; neel:01 krishnaswami:01 neelk:01 caml-list:01 hodgson:01 recursion:01 printf:01 compiler:01 rec:01 writes:01 binding:03 recursive:03 let:04 interaction:05 indefinitely:05 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Garry Hodgson 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? It's the simplest way of dealing with the interaction of lexical scope and recursion. Consider the following examples: let f = fun x -> (Printf.printf "#"; x) in let f x = f x in f 5 versus let f = fun x -> (Printf.printf "#"; x) in let rec f x = f x in f 5 The reference to 'f' in the second function body refers to the f already in scope. The 'rec' keyword is how you tell the compiler to ignore that and make it a recursive binding. So the first example prints "#" and return 5. The second loops indefinitely. -- Neel Krishnaswami neelk@alum.mit.edu ------------------- 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