[racket] Simplified scut-like macro

From: Gary Baumgartner (gfb at cs.toronto.edu)
Date: Thu Feb 23 01:29:22 EST 2012

#lang racket

(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)))
       (let ([y? (identifier-binding #'y)]
             [z? (identifier-binding #'z)])
                #`(case-lambda [(x) (letrec (#,@(if y? '() (list #'(y y)))
                                             #,@(if z? '() (list #'(z z))))
                                               body rest ...)]
                               [(x y) (letrec (#,@(if z? '() (list #'(z z))))
                                        body rest ...)]
                               [(x y z) body rest ...]))))))

(map (fun x) '(1 2 3))
(map (fun (+ x x)) '(1 2 3))
(map (fun (+ x y)) '(1 2 3) '(1 2 3))
(define y 10)
(map (fun (+ x y)) '(1 2 3))
(map (fun (+ x z)) '(1 2 3))

Posted on the users mailing list.