> > (define-syntax list-of-tail
> (syntax-rules (in)
> ((list-of-tail base expr)
> (cons expr base))
> ((list-of-tail base expr (var in generator) rest ...)
> (let* ((z (gensym))
> (f (lambda (z var) (list-of-tail z expr rest ...))))
> (fold-left f base generator)))
> ((list-of-tail base expr pred? rest ...)
> (if pred? (list-of-tail base expr rest ...) base))))
I don't think that (z (gensym)) is doing what you think it is doing.
Try leaving it out, and you'll see that it wasn't doing anything.
> > (define (pyth n)
> (list-of (list a b c)
> (a in (upto 1 n))
> (b in (upto a n))
> (c in (upto b n))
> (= (+ (* a a) (* b b)) (* c c))))
> > (pyth 20)
> ((3 4 5) (5 12 13) (6 8 10) (8 15 17) (9 12 15) (12 16 20))
Ain't macros fun? I seem to recall SICP1 used a macro named COLLECT
similar to this, but they didn't show how to implement it.
-al
(define (fold-left f b l)
(if (null? l) b (fold-left f (f b (car l)) (cdr l))))
(define-syntax list-of
(syntax-rules ()
((list-of expr ...)
(reverse (list-of-tail '() expr ...)))))
(define-syntax list-of-tail
(syntax-rules (in)
((list-of-tail base expr)
(cons expr base))
((list-of-tail base expr (var in generator) rest ...)
(let* ((f (lambda (z var) (list-of-tail z expr rest ...))))