Sleeper wrote:this recursive version is better
...
Yes! This problem screams for a recursive solution. For those about to recurse, I salute you! :-)
BTW, you don't have to shadow the variable
x by way of your function's parameter list, as
x is already locally scoped by the
dolist.
Also, the body of your function looks like a framework for a nice abstraction;
remove-all can be an instance of that abstraction:
Code: Select all
(define (walk-dir ffunc dfunc root)
"Walk a directory tree at `root' and apply `dfunc' to
directories and `ffunc' to files."
(dolist (x (directory root))
(let (full (append root "/" x))
(if (directory? full)
(when (and (!= x "..") (!= x "."))
(walk-dir ffunc dfunc full)
(dfunc full))
(ffunc full)))))
(define remove-all (currie walk-dir delete-file remove-dir))
Of course, you'll need the following handy macros.
Code: Select all
(define-macro (when)
(letex ($test (eval (args 0))
$expr (cons 'begin (1 (args))))
(if $test $expr)))
(define-macro (currie f)
(letex ($f (eval f)
$cargs (map eval (args)))
(lambda () (apply $f (append (quote $cargs) (args))))))
(λx. x x) (λx. x x)