[plt-scheme] Use of empty in macro definition

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Jul 17 16:24:53 EDT 2008

On Jul 17, wooks wrote:
> My first foray into macros. Practicing with some exercises.
> (require (lib "defmacro.ss"))
> (define-macro thunk-it
>   (lambda (code)
>     (cons 'lambda (cons empty (list  code)))))
> 
> gives
> 
>  reference to undefined identifier: empty
> 
> If i replace empty with '() it works.

This code uses `empty', which you need to have at the syntax level:

  (require (for-syntax scheme/list))

But if you want to start learning macros, it is almost always better
to use `syntax-rules'.  In your case, you want to turn a form like

  (thunk-it <expr>)

into a thunk expression:

  (lambda () <expr>)

The nice thing about `syntax-rules' is that it allows you to write
just this transformation specification:

  (define-syntax thunk-it
    (syntax-rules ()
      [(thunk-it expr) (lambda () expr)]))

And using this you get the usualy hygiene benefits, for example:

  > (define twelve (let ([lambda list]) (thunk-it 12)))
  > (twelve)
  12

This wouldn't work with `define-macro'.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.