[racket] Simplified scut-like macro

From: Paul Pereira (pjspereira at gmail.com)
Date: Wed Feb 22 22:11:20 EST 2012

I am new to macro writing and am trying to do the following:

(fun body ...)
 =>  (lambda (x) body ...)
or (lambda (x y) body ...)
or (lambda (x y z) body ...)

where x, y, z are taken from the scope of body.
Right now, I am just trying to use case-lambda to switch between
these. I guess I am trying for a much simpler version of scut (
https://github.com/jeapostrophe/exp/blob/master/scut.ss ), but I don't
understand that macro yet.

What I have so far does not work:

(define-syntax (fun stx)
  (syntax-case stx ()
    ((fun body rest ...)
     (with-syntax ((x (datum->syntax #'body 'x))
                   (y (datum->syntax #'body 'y))
                   (z (datum->syntax #'body 'z)))
        #'(case-lambda ((x) body rest ...)
                       ((x y) body rest ...)
                       ((x y z) body rest ...))))))

(map (fun x) '(1 2 3))  => '(1 2 3)    ... ok
(map (fun (+ x x)) '(1 2 3))  => '(2 4 6) ... ok
(map (fun (+ x y)) '(1 2 3) '(1 2 3))  ... y is an unbound identifier

Do you have any suggestions? Thanks!


Posted on the users mailing list.