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 RAA01288; Sat, 7 Jun 2003 17:09:19 +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 RAA01079 for ; Sat, 7 Jun 2003 17:09:18 +0200 (MET DST) Received: from ux9.sp.cs.cmu.edu (UX9.SP.CS.CMU.EDU [128.2.220.166]) by concorde.inria.fr (8.11.1/8.11.1) with SMTP id h57F9HH23264 for ; Sat, 7 Jun 2003 17:09:17 +0200 (MET DST) Received: from 12-203-128-226.client.attbi.com ([12.203.128.226]) by ux9.sp.cs.cmu.edu id aa15014; 7 Jun 2003 11:08 EDT Received: from ecc by stratocaster.home with local (Exim 3.36 #1 (Debian)) id 19OfJO-0005HZ-00 for ; Sat, 07 Jun 2003 11:08:54 -0400 Date: Sat, 7 Jun 2003 11:08:54 -0400 To: caml-list@inria.fr Subject: Re: [Caml-list] Why do input* and readdir throw End_of_file ... annoying! Message-ID: <20030607150854.GA20233@ecooper.com> Mail-Followup-To: caml-list@inria.fr References: <20030606170131.GA2769@redhat.com> <32CAF7EC-9849-11D7-BAD2-000393863F70@exomi.com> <20030606184304.GA3396@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030606184304.GA3396@redhat.com> User-Agent: Mutt/1.5.4i From: "Eric C. Cooper" X-Spam: no; 0.00; caml-list:01 readdir:01 pathname:01 opendir:01 closedir:01 rec:01 filesystem:02 06,:02 match:02 exception:02 wrote:03 annoying:03 let:04 accumulator:05 define:05 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Fri, Jun 06, 2003 at 07:43:04PM +0100, Richard Jones wrote: > The problem is that there doesn't seem to be a way to write the > loop function using readdir. eg: > > let rec loop () = > let filename = readdir dirh in > match filename with > | "." -> loop () > | ".." -> loop () > | filename -> > let pathname = path ^ "/" ^ filename in > let stat = lstat pathname in > let this = if stat.st_kind = S_DIR then > read_directory pathname > else > File pathname in > this :: loop () > in > try > Directory (loop ()) > with > End_of_file -> XXX what? > > Because the exception is always raised (it's not an exception at all) > there's no way to return the result of the call to loop (). Define your loop to take an accumulator (a list of the results so far): let rec read_filesystem path = if (lstat path).st_kind = S_DIR then Directory (read_directory pathname) else File path 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 -- Eric C. Cooper e c c @ c m u . e d u ------------------- 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