On Oct 6, 8:39 am, B Smith-Mannschott <bsmith.o...@gmail.com> wrote:
> On Wed, Oct 6, 2010 at 08:49, Abraham <vincent....@gmail.com> wrote:
> > ; prints all files
> > (import 'java.io.File)
> > (defn walk [dirpath]
> >  (doseq [file (-> dirpath File. file-seq)]
> >     (println (.getPath file)  )))
>
> This doesn't do what you said you wanted to do above: list all files,
> recursively. It just lists the files contained directly in dirpath.

file-seq is based on tree-seq and does indeed return all files and
directories recursively (and lazily).

> Listing files recursively is a tree recursion. A tree recursion is not an
> iterative process. loop/recur won't help you there. You'll need to use real
> stack-consuming recursion.

Just to be a devil's advocate, a tree recursion can be translated to
loop/recur using an explicit stack:

(defn list-files [dirname]
  (loop [stack [(java.io.File. dirname)]]
    (when-let [f (peek stack)]
      (println (.getPath f))
      (recur (into (pop stack) (.listFiles f))))))

Changing the stack to a queue would make the traversal happen breadth-
first rather than depth-first.

Justin

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to