The pattern matcher in `syntax-case` is less powerful than the one in
`syntax-parse`. The pattern

  (_ trash-left ... save-the-world . trash-right)

can match `save-the-world` only against the last item in a list --- or
against the first of the last pair for a non-list.


With `syntax-parse`, as you've probably noticed, you can write

  (_ trash-left ... save-the-world trash-right ...)

and it will find a match in all of your examples. With `syntax-case`,
that pattern is not even allowed.


At Wed, 27 Jan 2016 23:11:25 -0800 (PST), reilithion wrote:
> I was trying to write a macro today (I'm not too good at writing macros yet) 
> and I ran up against a wall when I realized that syntax-case wasn't behaving 
> the way I expected. I boiled down the behavior to the following test case:
> 
> #lang racket/base
> 
> (require (for-syntax racket/base))
> 
> ;; Intended behavior:
> ;; Break the world unless save-the-world is
> ;; ANYWHERE in the list of arguments
> (define-syntax (break-the-world stx)
>   (syntax-case stx (save-the-world)
>     [(_ trash-left ... save-the-world . trash-right)
>      #'(begin (display 'hooray) (newline))]
>     [_ #'(error "The world is broken!")]))
> 
> (break-the-world save-the-world)
> (break-the-world 'trash1 save-the-world)
> (break-the-world save-the-world 'trash2)
> (break-the-world 'trash1 save-the-world 'trash2)
> 
> The first two expansions of break-the-world result in "hooray". The last two, 
> on the other hand, break the world. I gather that 'trash2 is not being 
> matched 
> by the ". trash-right" part of my pattern. What I don't understand is why, 
> especially when this kind of thing seems to work if I use syntax-parse 
> instead.
> 
> Is there a more idiomatic way of matching a pattern anywhere in a list?
> 
> Thanks!
> Lucas Paul
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to