From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA11161; Fri, 6 Jun 2003 20:32:42 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA11156 for ; Fri, 6 Jun 2003 20:32:41 +0200 (MET DST) Received: from epexch01.qlogic.org ([63.170.40.3]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h56IWcT27800 for ; Fri, 6 Jun 2003 20:32:40 +0200 (MET DST) Received: from epmailtmp.qlogic.org ([10.20.33.254]) by epexch01.qlogic.org with Microsoft SMTPSVC(5.0.2195.5329); Fri, 6 Jun 2003 13:33:59 -0500 Received: from [10.20.33.146] ([10.20.33.146]) by epmailtmp.qlogic.org with Microsoft SMTPSVC(5.0.2195.4905); Fri, 6 Jun 2003 13:33:58 -0500 Date: Fri, 6 Jun 2003 13:49:55 -0500 (CDT) From: Brian Hurt X-X-Sender: Reply-To: Brian Hurt To: Ville-Pertti Keinonen cc: Richard Jones , Subject: Re: [Caml-list] Why do input* and readdir throw End_of_file ... annoying! In-Reply-To: <32CAF7EC-9849-11D7-BAD2-000393863F70@exomi.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-OriginalArrivalTime: 06 Jun 2003 18:33:58.0852 (UTC) FILETIME=[31ABB040:01C32C5A] X-Spam: no; 0.00; qlogic:01 caml-list:01 readdir:01 permissive:01 recursion:01 beginners:01 accum:01 compiler:01 ocaml:01 approaches:01 rec:01 overflow:02 match:02 stack:02 o'caml:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Fri, 6 Jun 2003, Ville-Pertti Keinonen wrote: > Since exceptions are reasonably inexpensive in O'Caml, and used for > things such as Not_found in basic collection types, using exceptions to > indicate the end of some particular set of values seems reasonable, to > me. > > Writing a try ... with statement isn't really that different from a > match statement except for the fact that you can handle exceptional > circumstances generated by several expressions in the with ... part of > a try ... with statement - if anything, that's more permissive. > The problem with try/with is that it's way to easy to break tail recursion using try/with. About every other week someone comes to the Ocaml beginners list where they are doing something like: let rec read_all_lines chan accum = try let line = input_line chan in read_all_lines chan (line :: accum) with End_of_file -> List.rev accum Unfortunately, this is *not* tail recursive. Works fine on small files, but when they try to run it on large files they stack overflow. Which means you need to contort to take the recursion out of the try/with block. Several different approaches to this are possible- but none with the obviousness of the above code. Personally, I'd like the following to be an efficient way to read all the lines from a file into a list: let rec read_all_lines chan = try let line = input_line chan in line :: (read_all_lines chan) with End_of_file -> [] and have the compiler contort the code into an efficient, tail-recursive form, with a combination of data structures with a hole and catch removal. 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