Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Alan Schmitt <alan.schmitt@i...>
Subject: Re: [Caml-list] Stack Overflow... (recursion in try-statement)
* Oliver Bandel (oliver@first.in-berlin.de) 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 =
  try 
    let d = Unix.readdir dir in
    d :: (traversedir dir)
  with
  | 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.

HTH,

Alan


-- 
The hacker: someone who figured things out and made something cool happen.
-------------------
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