Tuesday, April 15, 2008

Back-tick-style macros in Scheme

Somebody has been lying to me.

Observe a contrived Common Lisp macro:

(defmacro print-line (x)
`(format "~a~%" ,x))

Here's a similar macro in Scheme:

(define-macro (print-line x)
(display ,x)

Apparently, nobody told me that back-tick-style macros were supported in Scheme. In fact, all I've heard about Scheme macros is stuff about unneeded "pattern matching" involved in using macros.

The people who have claimed this appear to be lying scumbags. Scheme has the back-ticks, the comma, the comma-at, even GENSYM, meaning it's pretty much capable of the same simple macro style that Common Lisp users are used to.

Maybe there's more to it. Hygienic macros and that pattern matching stuff fit somewhere in Scheme.


Anonymous said...

I was equally excited, but its not quite the same as in CL. For MzScheme at least, the following generates an error because define-macro doesn't have access to your definitions.

(define (square x) (* x x))
(define-macro (add-square x)
`(+ ,x ,(square x)))

I'm still trying to get a better understanding of Scheme macros to see if I can use runtime information in writing macro's - there are things I want to do which would certainly be much harder or not possible without access to user defined code...

tunginobi said...

Damn, that sucks.