Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] looping recursion
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Prevost <j.prevost@g...>
Subject: Re: [Caml-list] looping recursion
On Tue, 27 Jul 2004 16:43:10 -0700, briand@aracnet.com
<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