Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Why do input* and readdir throw End_of_file ... annoying!
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: art yerkes <ayerkes@o...>
Subject: Re: [Caml-list] Why do input* and readdir throw End_of_file ... annoying!
On Sat, 7 Jun 2003 13:27:11 -0700
David Brown <caml-list@davidb.org> 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.
> 
> Dave



how about:

open Unix

let read_fs starting_dir =
  let rec go_until_exception list_ref f =
    list_ref := (f ()) :: !list_ref ; go_until_exception list_ref f
  and dirs_ref = ref [] in
  let the_dir = opendir starting_dir in
  try
    go_until_exception dirs_ref (fun () -> readdir the_dir)
  with End_of_file -> !dirs_ref
 
> 
> -------------------
> 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


-- 
`No, you don't understand,' the Knight said, looking a little vexed. 
`That's what the name is called. The name really is "The Aged Aged 
Man."'
-- Lewis Carroll

-------------------
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