[racket] Problem with macro

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Fri Aug 12 11:44:40 EDT 2011

> Oh, that's my problem: i obviously don't understand what's the places where
> macro expansion happens.
> I ended up writing "classical" lisp non-hygienic macro instead.
>
>  It's a sad thing that I cannot find a book or text as clear as PG's "On
> Lisp" that covers Racket's hygienic macros in such detailed manner. :(


Personally, I rarely use pure template-based macros; I mostly use the
syntax-case system.  It's closer to the "old-fashioned" way of doing
things.  In the old fashioned way, your macros are functions that take
in s-expression and return s-expressions.  In Racket's syntax-case
system, your macros are functions that take in syntax and return
syntax.

For example:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket
(define-syntax (with stx)
  (syntax-case stx ()
    [(_ name value body ...)
     (begin
       (printf "I am compiling: ~s\n" stx)
       (printf "I have destructed the syntaxes: ~s\n~s\n~s\n"
               #'name
               #'value
               #'(body ...))
       (let ([result
              #'(let ([name value])
                  body ...)])
         (printf "I am going to return ~s to the compiler.\n" result)
         result))]))

;; Trying the macro out:
(with x 3 (+ x x))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Just as ' is an abbreviation for quote, #' is an abbreviation for
syntax.  Kent Dybvig's tutorial on syntax case was helpful for me:

    http://www.cs.indiana.edu/~dyb/pubs/tr356.pdf

It would be nice if there were a document that consolidated the
content of that, but put in the context of Racket rather than just
generally Scheme.



Posted on the users mailing list.