[plt-scheme] define-syntax question
I see the problem now. syntax-rules requires the pattern to start with the
keyword for the macro (here, "label"), and that won't work in this case.
I'm not sure how to achieve the effect I want. I want to be able to say
((label fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1))))))
10)
and get 10 factorial.
BTW it's possible that the common lisp term is "labels"; I'm not sure.
This may also be an archaic old lisp form.
Mike
> From: Eli Barzilay <eli at barzilay.org>
> Date: Thu, 26 Jun 2003 22:51:42 -0400
>
> On Jun 26, Michael Vanier wrote:
> > I'm trying to come up with a scheme equivalent for a restricted
> > version of common lisp's "label" construct, but I'm getting an error
> > I don't understand. Here is my code:
> >
> > (define-syntax label
> > (syntax-rules ()
> > (((_ f (lambda (p1 p2 ...) e)) a1 a2 ...)
> > (letrec ((f (lambda (p1 p2 ...) e)))
> > (f a1 a2 ...)))))
>
> Looks like a case of bad parens, shouldn't that be
>
> (define-syntax label
> (syntax-rules ()
> ((_ f (lambda (p1 p2 ...) e) a1 a2 ...)
> (letrec ((f (lambda (p1 p2 ...) e)))
> (f a1 a2 ...)))))
>
> ?
>
> Even that is very likely not right -- do you really want to have a
> `lambda' as part of the pattern? If so, you should add it to the
> first syntax-rules argument. But that doesn't make much sense, so it
> is probably better to match on all values:
>
> (define-syntax label
> (syntax-rules ()
> ((_ f func a1 a2 ...)
> (letrec ((f func))
> (f a1 a2 ...)))))
>
> but that doesn't look like something useful which gets back to the
> question of what exactly is CL's `label'? AFAIK, it doesn't have one.
>
> --
> ((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
> http://www.barzilay.org/ Maze is Life!
>