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] Stack Overflow... (recursion in try-statement)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-04-24 (04:53)
From: Alan Schmitt <alan.schmitt@i...>
Subject: Re: [Caml-list] Stack Overflow... (recursion in try-statement)
* Oliver Bandel ( wrote:
> Hello,
> why does an stack overflow-error occur here?
> let rec traversedir dir =
>           try ( [Unix.readdir dir] @ traversedir dir ) with
>           End_of_file -> [];;
> I have not tried it with very deep directories, so
> I did not expect such an error...
> What is the problem here?

I think you stumbled on the good old "evaluation order is not specified"
thingy ... If the right hand side of the append is called before the
left hand side, it is not going to work. How about this code:

let rec traversedir dir =
    let d = Unix.readdir dir in
    d :: (traversedir dir)
  | End_of_file -> []

(Disclaimer: I didn't test the code). The idea is you force the
evaluation of readdir before the recursive call.

I also modified the [d] @ l into a d :: l, which seems to be the same
thing to me.



The hacker: someone who figured things out and made something cool happen.
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: