[plt-scheme] Re: Macros and Matching

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Fri Jul 13 07:49:16 EDT 2007

Paulo J. Matos skrev:
> On 13/07/07, Paulo J. Matos <pocm at soton.ac.uk> wrote:
>> Just found why doesn't work. From R5RS:
>> "The keyword at the beginning of the pattern in a <syntax rule> is not
>> involved in the matching and is not considered a pattern variable or
>> literal identifier."
>> Any ideas on the best way to solve this?

Well, syntax-case matches the keyword in the beginning, so replacing
syntax-rules with syntax-case does the trick:

(module test mzscheme

   (define-syntax (machine stx)
     (syntax-case stx ()
       [(_ mname sexps ...)
        (identifier? #'mname) ; fender
        (let* ([sections (syntax->list #'(sexps ...))]
                (map (lambda (section)
                       (syntax-case section (variables properties)
                         ((variables (name type value) ...)
                          #'(begin (printf "Var ~a : ~a -> ~a~n"
                                           'name 'type 'value) ...))
                         ((properties formula ...)
                          #'(begin (printf "Prop ~a~n" 'formula) ...))))
              #, at parts))]))

   (machine test
             (a integer 0)
             (b boolean false))
             (=> a b))))

;;; Output

Var a : integer -> 0
Var b : boolean -> false
Prop (=> a b)

Jens Axel Søgaard

Posted on the users mailing list.