An alternate syntax for let

In this post I wondered how one might implement an alternate syntax for let (described in this C.L.L. post), which looks like this:

(let (x 0 y 1 z 2) (+ x y z))

As it turns out my solution was actually let*, and therefore wrong.

Nonetheless, here is wonderful macro provided by Jos that both implements the alternate syntax for let and also demonstrates a technique for how to use the equivalent of “temporary variables” inside of a macro:

(define-syntax my-let
  (syntax-rules ( )
    ((_ my-bindings body-expr0 body-expr ...)
     (my-let-aux my-bindings ( ) body-expr0 body-expr ...))))

(define-syntax my-let-aux
  (syntax-rules ( )
    ((_ ( ) (binding ...) body-expr0 body-expr ...)
     (let (binding ...) body-expr0 body-expr ...))
    ((_ (var value-expr . rest) (binding ...) body-expr0 body-expr ...)
     (my-let-aux rest (binding ... (var value-expr)) body-expr0 body-expr ...))))

(my-let (x 1 y 2 z 3)
        (+ x y z))

Leave a Reply

Your email address will not be published. Required fields are marked *