[plt-scheme] macro help

From: Stephen Chang (stchang at ccs.neu.edu)
Date: Mon May 24 13:14:34 EDT 2010

I've also been trying to learn about programming with macros recently
and the sources I've found most useful are PLAI, TSP chapter 8, and
Dybvig's syntax-case guide:

http://www.cs.indiana.edu/~dyb/pubs/tr356-abstract.html

The guide and the TSP chapter have some overlap but I found both to be
a worthwhile read anyways.


The guides linked to from here (which includes Neil's link):

http://community.schemewiki.org/?scheme-faq-macros

are also informative but they only talk about syntax-rules.





On Sun, May 23, 2010 at 9:15 AM, Todd O'Bryan <toddobryan at gmail.com> wrote:
>
> I think I've crossed the border between syntax-rules and syntax-case,
> am floundering a bit, and would appreciate any help people would be
> willing to provide.
>
> I want to design a structure that has required fields and optional
> fields with defaults and I want to be able to include contract
> information in the structure definition. Something like
>
> (define-my-struct blah
>  ([required1 symbol?]
>   [required2 number?]
>   [optional1 symbol? 'default]
>   [optional2 boolean? #f]))
>
> I'd then rewrite this as a traditional define-struct, a
> provide-contract, and an extra constructor-like function that uses
> keyword arguments so that I can create structures and set values of
> only the fields I care about.
>
> I've gotten as far as
>
> (define-syntax define-my-struct
>  (syntax-rules ()
>    ([(_ name (field-info ...))
>
> which I think will match 'blah to name and '(required1 symbol?) to
> each field-info.
>
> So, now I need to pull the first out of each field info and put it
> into a define-struct. Something like
>
> (define-struct name (for/list ([fld-list (list field-info ...)])
> (first fld-list)))
>
> but that obviously doesn't work because I'm conflating a couple of
> phase-levels. I think I probably need to switch to syntax-case and
> have the macro return a syntax object, but I'm in over my head.
>
> Thanks,
> Todd
>
> P.S. Does anyone have a series of exercises that guide one through the
> complexities of macro writing? PLAI has a great section on macros that
> I just re-read last night, but it doesn't talk about how you pull out
> pieces from the pattern and re-combine them in the template. (Or maybe
> it does and I just missed it.)
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.