[plt-scheme] a simple macro

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Tue Nov 21 20:40:45 EST 2006

On Tue, 21 Nov 2006, michael rice wrote:

> Here's a macro that uses a user defined function in
> its expansion.

Hi Michael,

Here you go:

(module example-macro mzscheme

   (define-for-syntax (fun n)
     (cond [(= n 0) '()]
            (cons n (fun (sub1 n)))]))

   (define-syntax (mac stx)
     (syntax-case stx ()
       [(_ n)
        (quasisyntax/loc stx
          (+ #,@(fun (syntax-object->datum #'n))))])))

There's a nice description of the macro system Matthew Flatt's Composable 
and Compilable Macros slides:


and a more detailed explanation in:


There's also a nice section in the Schematics Cookbook:


Hope this helps!

Posted on the users mailing list.