From: rif
Subject: directory recursion
Date: 
Message-ID: <wj0n0oao536.fsf@five-percent-nation.mit.edu>
What I want (and have) is a function that recurses over a directory
tree, finding all functions that satisfy a match predicate and forming
some action on them.  My previous version relied mostly on treating
files as strings (i.e., it was a directory if the namestring ended in
"/").  I wrote a new version that uses pathnames:

(defun directoryp (p)
  (equal (pathname p) (make-pathname :directory (pathname-directory p))))

(defun to-directory (p)
  (if (pathname-name p)
      (make-pathname :directory (append (pathname-directory p)
                                        (list (pathname-name p))))
      p))

(defun recurse-directory (pathname action
                          &key (match #'identity) (print-dir t))
  (let ((p (to-directory pathname)))
    (when print-dir (format t "Entering ~A~%" (namestring p)))
    (let ((d (directory pathname)))
      (mapcar #'(lambda (fp)
                  (if (directoryp fp)
                      (recurse-directory fp action :match match
                                         :print-dir print-dir)
                      (when (funcall match fp) (funcall action fp))))
              d)))
  nil)

Is there anything here that sets off warning bells?  In particular,
are here easier/better ways to define the two helper functoins
directoryp or to-directory?  Also, is there some built-in in Lisp that
does all this better that I just don't know about?

Cheers,

rif

ps.  Working with pathname-type sure beats trying to parse the
suffixes as strings.  Thanks to everyone who suggested moving to
pathnames.

From: rif
Subject: Re: directory recursion
Date: 
Message-ID: <wj0isyyv1gi.fsf@five-percent-nation.mit.edu>
Doh.

Typo in the function.  It should of course be (let ((d (directory p)))

rif
From: Adam Warner
Subject: Re: directory recursion
Date: 
Message-ID: <pan.2002.11.16.00.07.07.569370@consulting.net.nz>
Hi rif,

> Also, is there some built-in in Lisp that does all this better that I
> just don't know about?

Perhaps :wild-inferiors in directory pathnames that recurse over
subdirectories. In namestrings/pathnames they are also denoted by a double
asterix, **

Once I discovered wild inferiors I didn't need to perform any explicit
recursion. But note that an implementation doesn't have to support them:

"19.2.2.4.3 Restrictions on Examining a Pathname Directory Component"
http://www.lispworks.com/reference/HyperSpec/Body/19_bbdc.htm

So you might be better off supporting recursion explicitly.

Regards,
Adam