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