[plt-scheme] How to introduce a variable binding into a macro?

From: Grant Rettke (grettke at acm.org)
Date: Sat Sep 1 02:15:47 EDT 2007

Hi I am looking at simple macros.

Here is a macro to create a one argument function.

(define-syntax =>
  (syntax-rules {}
    {(_ var body)
     (λ (var) body)}))

(define foo1 (=> x (+ x x)))

I would like to abstract that for creating one arguments functions,
where x is the variable bound to the argument of the function. I would
like to be able to refer to that variable in the body of the function

First I tried this:

(define-syntax \x
  (syntax-rules {}
    {(_ body)
     (λ (x) body)}))

(define foo2 (\x (+ x x)))

That doesn't work as (I'm guessing) the body doesn't have 'x' within
its scope. I would like to bind the argument of that function as x and
introduce it to the scope of its body. The error message just says
'bad syntax'.

It seems like with-syntax and datum->syntax-object is a place to start:


I'm not too sure where to start, though. Any tips on this one?

Posted on the users mailing list.