[plt-scheme] sytnax macro question

From: David Van Horn (dvanhorn at cs.uvm.edu)
Date: Sun May 11 15:37:36 EDT 2003

John W. Small wrote:

>> (define-syntax test 
>      (lambda (stx)
>        (syntax-case (list (- 2 1) (+ 1 1) (+ 2 1)) ()
>          ((x y ...)
>              (display (syntax-object->datum
>                (syntax (x y ...))))
>   ;           (newline)
>              (syntax (list x y ...))))))
>  > (test)
>  (1 2 3)     ; output displayed
>  (1 2 3)     ; returned value
> 
> However if the (newline) is uncommented leaving 3 statements in the case
> clause the following error occurs.
>
>    syntax-case*: bad clause in: ((x y ...) (display (syntax-object->datum
> (syntax (x y ...)))) (newline) (syntax (list x y ...)))
>
> Is this correct behavior?

Yes.

 From The Scheme Programming Language page 168:  http://www.scheme.com/tspl2d/

   (syntax-case exp (literal ...) clause ...)                syntax

   Each /literal/ must be an identifier.  Each /clause/ must take one of the
   following two forms:

   (pattern output-expression)
   (pattern fender output-expression)

So in your case (display ...) is considered a fender and the syntax-case is
well-formed (although perhaps not what you intended).  The fender is
evaluated, which prints output and returns an undefined value that is
interpreted as true.  So the output-expression is returned.

When you add another expression to the clause, the syntax-case is no longer
well-formed.  As the error message states, you have a "bad clause".

So just use (begin ...) or (let () ...) as you had in the original example.

HTH.

-d




Posted on the users mailing list.