[plt-scheme] macro help

From: Todd O'Bryan (toddobryan at gmail.com)
Date: Sun May 23 09:15:40 EDT 2010

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.)


Posted on the users mailing list.