From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA20073 for caml-redistribution; Mon, 27 Jul 1998 20:49:36 +0200 (MET DST) 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 UAA19999 for ; Mon, 27 Jul 1998 20:30:44 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id UAA06084; Mon, 27 Jul 1998 20:30:38 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA24446; Mon, 27 Jul 1998 20:30:38 +0200 (MET DST) From: Pierre Weis Message-Id: <199807271830.UAA24446@pauillac.inria.fr> Subject: Re: Let rec trouble To: oliver@fritz.traverse.com (Christopher Oliver) Date: Mon, 27 Jul 1998 20:30:38 +0200 (MET DST) Cc: caml-list@inria.fr In-Reply-To: from "Christopher Oliver" at Jul 25, 98 03:06:38 pm X-Mailer: ELM [version 2.4 PL24 ME8] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: weis > I'm having trouble with the syntax of let rec. Consider the following > program for computing Van der Waerden's bound: > > open Num > open Nat > open Big_int > open Ratio > > let rec n k l = > let rec m i = > if i =/ Int 0 then > Int 1 else > Int 2 > */ (m (pred_num i)) > */ (n (k **/ (m (pred_num i))) (pred_num l)) in > if l =/ Int 2 then succ_num k else m k;; > > print_string (string_of_num (n (Int 3) (Int 3)));; > > I would like to restrict the lexical scope of 'n' by replacing the first > double semicolon with 'in.' I nest m precisely to capture k and l in m's > lexical environment. Why is this use forbidden? I.e. Why shouldn't I be > able to write: > > let rec n k l = > let rec m i = > if i =/ Int 0 then > Int 1 else > Int 2 > */ (m (pred_num i)) > */ (n (k **/ (m (pred_num i))) (pred_num l)) in > if l =/ Int 2 then succ_num k else m k > in > print_string (string_of_num (n (Int 3) (Int 3)));; > > I would prefer not to define a top level symbol, and this seems an > inconsistancy. Am I missing something? > > -- > Christopher Oliver Traverse Internet > Systems Coordinator 223 Grandview Pkwy, Suite 108 > oliver@traverse.net Traverse City, Michigan, 49684 > let magic f = fun x -> x and more_magic n f = fun x -> f ((n f) x);; You're second program is perfectly legal in Caml (and Objective Caml) and indeed works fine. The problem (if any) is that this program leads to a huge computation (the result is an integer number with 50100 decimal digits). Thus it takes a while to run, and that may be the reason why you thought the second version did not work properly: in the first version, when defining the n function first you had not to wait for any computation. You just have to wait for the answer when evaluating print_string (string_of_num (n (Int 3) (Int 3)));;. On the other hand, when mixing the final computation and the function definition you had to wait until the end of the entire computation. I assume you were using the interactive system, hence your surprise. If you were writing stand alone programs, the compiler whould have properly compiled the two versions of your program and you would never have noticed any problem! Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/