Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2004-07-28 (00:27)
From: John Prevost <j.prevost@g...>
Subject: Re: [Caml-list] looping recursion
On Tue, 27 Jul 2004 16:43:10 -0700,
<> 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

Here's an example of a loop that makes that obvious:

exception Boom

let rec loop x =
    if x < 100 then loop (x + x) else raise Boom
  with Boom -> x

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: