[plt-scheme] define-syntax question

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Jun 26 23:31:21 EDT 2003

On Jun 26, Michael Vanier wrote:
> 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.

How about:

  (define-syntax label
    (syntax-rules ()
      ((label name func) (letrec ((name func)) name))))

> BTW it's possible that the common lisp term is "labels"; I'm not
> sure.  This may also be an archaic old lisp form.

Well, `labels' is just like `letrec', except that using it doesn't
require `lambda', something like:

  (define-syntax labels
    (syntax-rules ()
      ((labels ((name args b0 b ...) ...) body0 body ...)
       (letrec ((name (lambda args b0 b ...)) ...) body0 body ...))))

and you'd use it like this:

  (labels ((fact (n) (if (= n 0) 1 (* n (fact (- n 1)))))) (fact 10))

The other CL issue is that the binding is actually a function binding,
but that's an issue for a different flamewar.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.