[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) '()]
           [else
            (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:

     http://www.cs.utah.edu/plt/slideshow/macro-module-slides/mmtalk.pdf

and a more detailed explanation in:

     http://www.cs.utah.edu/plt/publications/macromod.pdf

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

     http://schemecookbook.org/Cookbook/GettingStartedMacros


Hope this helps!


Posted on the users mailing list.