[racket] Macro + function automation

From: Roman Klochkov (kalimehtar at mail.ru)
Date: Thu Jul 11 00:09:27 EDT 2013

I like to make syntax-optimized functions.

(define-syntax (square stx)
  (syntax-case stx (square expt)
    [(square (square expr)) #'(expt expr 4)]
    [(square (expt expr n)) (if (number? (syntax-e #'n))
                                                 #`(expt expr #,(* (syntax-e #'n) 2))
                                                 #'(expt expr (* n 2)))]
    [(square x) #'(* x x)]
    [square #'(lambda (x) (* x x))]))

So I can write  (square (expt x 5)) and have evaluate (expt x 10) and I can write (map square '(1 2 3)) and it is also works.

But I dislike to repeate the body of the function in last two cases.

I tryed  

(define-syntax (square stx)
  (let ([body #'(* x x)])
    (syntax-case stx (square expt)
      ...
      ...
      [(square x) body]
      [square #'(lambda (x) #`body)]))

but hygiene prevents me from that:  x: unbound identifier in module in: x How can I bind template body with some variable? Or how to rewrite the syntax to evade duplication?

-- 
Roman Klochkov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130711/9c4a26f6/attachment-0001.html>

Posted on the users mailing list.