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: briand@a...
Subject: [Caml-list] looping recursion

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 :

let read_file filename =
  let file = open_in filename in
  let rec proc line =
    print_string line;
    print_newline();
    try
      proc (input_line file);
    with
        End_of_file -> true;
  in
    proc (input_line 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.

let rec proc line =
...
in
try
 proc (input_line file);
with
 End_of_file -> true;

and that works just fine.

Is there a better way to just pull in all the lines of a file ?  I've
just gotten used to doing it like this, because in scheme it's very
clean.  And since I'm not actually accumulating anything, I'm most
confused as to how the stack space is disappearing.

Brian


-------------------
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