Browse thread
[Caml-list] Why do input* and readdir throw End_of_file ... annoying!
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Brian Hurt <brian.hurt@q...> |
| Subject: | Re: [Caml-list] Why do input* and readdir throw End_of_file ... annoying! |
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