caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] looping recursion
@ 2004-07-27 23:43 briand
  2004-07-28  0:27 ` John Prevost
  2004-07-28  0:37 ` skaller
  0 siblings, 2 replies; 44+ messages in thread
From: briand @ 2004-07-27 23:43 UTC (permalink / raw)
  To: caml-list


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


^ permalink raw reply	[flat|nested] 44+ messages in thread

end of thread, other threads:[~2004-07-31  5:37 UTC | newest]

Thread overview: 44+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-27 23:43 [Caml-list] looping recursion briand
2004-07-28  0:27 ` John Prevost
2004-07-28  0:38   ` John Prevost
2004-07-28  1:17     ` skaller
2004-07-28  1:05   ` briand
2004-07-28  1:43     ` Brian Hurt
2004-07-28  2:49       ` briand
2004-07-28  3:12         ` Brian Hurt
2004-07-28  3:20         ` Brian Hurt
2004-07-28  5:54         ` brogoff
2004-07-28  7:22           ` Alex Baretta
2004-07-28 16:38             ` brogoff
2004-07-28 19:40               ` Jon Harrop
2004-07-28 20:18                 ` Brandon J. Van Every
2004-07-29  6:01                   ` Alex Baretta
2004-07-28 21:22                 ` brogoff
2004-07-29  9:13                   ` Daniel Andor
2004-07-29  9:25                     ` Keith Wansbrough
2004-07-29  9:41                       ` Nicolas Cannasse
2004-07-29  9:57                       ` Xavier Leroy
2004-07-29 10:44                         ` Daniel Andor
2004-07-29 12:56                           ` brogoff
2004-07-29 10:11                     ` skaller
2004-07-29 12:41                     ` brogoff
2004-07-29  6:28               ` Alex Baretta
2004-07-29 14:58                 ` brogoff
2004-07-29 16:12                   ` Brian Hurt
2004-07-29 17:49                     ` james woodyatt
2004-07-29 19:25                       ` Brian Hurt
2004-07-29 20:01                         ` brogoff
2004-07-30  4:42                           ` james woodyatt
2004-07-29 17:44                   ` james woodyatt
2004-07-29 23:12                     ` skaller
2004-07-29 22:42                   ` Alex Baretta
2004-07-30  2:38                     ` Corey O'Connor
     [not found]                     ` <200407300136.14042.jon@jdh30.plus.com>
2004-07-30 12:45                       ` Alex Baretta
2004-07-30 17:07                     ` brogoff
2004-07-30 18:25                       ` [Caml-list] kaplan-okasaki-tarjan deque (was "looping recursion") james woodyatt
2004-07-30 21:20                         ` brogoff
2004-07-31  5:37                           ` james woodyatt
2004-07-28  7:27       ` [Caml-list] looping recursion skaller
2004-07-28 14:36         ` Brian Hurt
2004-07-28 22:05           ` skaller
2004-07-28  0:37 ` skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).