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 CAA17942; Wed, 28 Jul 2004 02:27:09 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA18688 for ; Wed, 28 Jul 2004 02:27:08 +0200 (MET DST) Received: from mproxy.gmail.com (rproxy.gmail.com [64.233.170.202]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i6S0R7EV025596 for ; Wed, 28 Jul 2004 02:27:08 +0200 Received: by mproxy.gmail.com with SMTP id 75so118866rnl for ; Tue, 27 Jul 2004 17:27:05 -0700 (PDT) Received: by 10.38.71.58 with SMTP id t58mr15367rna; Tue, 27 Jul 2004 17:27:05 -0700 (PDT) Message-ID: Date: Tue, 27 Jul 2004 20:27:05 -0400 From: John Prevost To: Ocaml Mailing List Subject: Re: [Caml-list] looping recursion In-Reply-To: <16646.59534.101085.662305@soggy.deldotd.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <16646.59534.101085.662305@soggy.deldotd.com> X-Miltered: at nez-perce with ID 4106F2DB.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; prevost:01 prevost:01 caml-list:01 recursion:01 2004:99 recursion:01 0700,:01 rec:01 overflow:02 exception:02 stack:02 stack:02 chunk:03 wrote:03 recursive:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tue, 27 Jul 2004 16:43:10 -0700, briand@aracnet.com wrote: > Well without an eof-object to check for (as in scheme) I've started > using the following code chunk to read all the lines out of a file : {...} > works great until you read enough lines and then you get: > > Stack overflow during evaluation (looping recursion?). > > naturally I suspected that the try was perhaps ruining the > tail-callness of the code, so I moved it outside of the recursive > proc, i.e. Yup--this is one of the big things you have to watch out for when doing tail loops. The easy way to think of it is "try always makes stack frames, so use try at the top level and then escape all the way out". In essence, if there's a try block around your expression, it's not in tail position (even if every try block will return the same thing.) Here's an example of a loop that makes that obvious: exception Boom let rec loop x = try if x < 100 then loop (x + x) else raise Boom with Boom -> x ------------------- 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