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 RAA22203; Mon, 9 Jun 2003 17:15:22 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA22260 for ; Mon, 9 Jun 2003 17:15:21 +0200 (MET DST) Received: from epexch01.qlogic.org ([63.170.40.3]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h59FFKH20753 for ; Mon, 9 Jun 2003 17:15:20 +0200 (MET DST) Received: from epmailtmp.qlogic.org ([10.20.33.254]) by epexch01.qlogic.org with Microsoft SMTPSVC(5.0.2195.5329); Mon, 9 Jun 2003 10:16:32 -0500 Received: from [10.20.33.146] ([10.20.33.146]) by epmailtmp.qlogic.org with Microsoft SMTPSVC(5.0.2195.4905); Mon, 9 Jun 2003 10:16:32 -0500 Date: Mon, 9 Jun 2003 10:33:17 -0500 (CDT) From: Brian Hurt X-X-Sender: Reply-To: Brian Hurt To: David Brown cc: Subject: Re: [Caml-list] Why do input* and readdir throw End_of_file ... annoying! In-Reply-To: <20030607202711.GA32043@davidb.org> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-OriginalArrivalTime: 09 Jun 2003 15:16:32.0509 (UTC) FILETIME=[1BF072D0:01C32E9A] X-Spam: no; 0.00; qlogic:01 caml-list:01 readdir:01 0400,:01 opendir:01 closedir:01 extlib:01 enum:01 rec:01 filesystem:02 match:02 eof:02 exception:02 wrote:03 annoying:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sat, 7 Jun 2003, David Brown wrote: > On Sat, Jun 07, 2003 at 11:08:54AM -0400, Eric C. Cooper wrote: > > > and read_directory path = > > let dirh = opendir path in > > let rec loop entries = > > try match readdir dirh with > > | "." | ".." -> loop entries > > | filename -> loop (read_filesystem filename :: entries) > > with End_of_file -> entries > > in > > let list = loop [] in > > closedir dirh; > > list > > But, this isn't tail recursive, so you might as well not pass the > argument and build up the list upon returning. Is there a way of making > it really tail recursive, while using the exception? It isn't too hard > with a list ref to accumulate the results. let read_directory path = let dirh = opendir path in let rec loop entries = let fname, eof = try (readdir dirh), false with End_of_file -> "", true in if eof then List.rev entries else match fname with | "." | ".." -> loop entries | _ -> loop (fname :: entries) in loop [] I will also take this opportunity to plug ExtLib. Using ExtLib you can remove the List.rev call, and do something like: let read_directory path = let dirh = opendir path in let rec f () = try match readdir dirh with | "." | ".." -> f() | s -> s with End_of_file -> raise No_more_elements in ExtList.of_enum (Enum.from f) Here f is not tail recursive- but it only recurses to get past "." and ".." files, so it'll never go more than 3 levels deep. If it bugs you, rewrite f in a manner to my first example. 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