[racket] syntax-parse question

From: Alexander D. Knauth (alexander at knauth.org)
Date: Wed Aug 6 13:47:47 EDT 2014


On Aug 6, 2014, at 1:10 PM, Kevin Forchione <lysseus at gmail.com> wrote:

> 
> On Aug 5, 2014, at 2:21 PM, Jens Axel Søgaard <jensaxel at soegaard.net> wrote:
> 
>> Is this a step in the right direction?
>> 
>> (define-syntax (x stx)
>> (syntax-parse stx
>>  [(_ (y ... (z ...) w ...))
>>   #'(xf (yf y ... (zf z ...) w ...))]))
>> 
>> The pattern (z ...) ... will match a sequence of lists such as (4 5 6) (7 8)
>> but it won't match (4 5 6) 7 8 from your example.
>> 
>> /Jens Axel
> 
> Closer. It doesn’t match something like ‘( 1 2 3 (4 5 6) 7 (8 9) 10), for instance. 

For that I think you want something like this:
(syntax-parse stx
  [(_ (~or (z ...)
           y)
      ...)
   #'(xf (yf y ... (zf z ...)))])

Either that or you can use my version of syntax-parse with pattern-expanders and use ~seq-no-order:
https://github.com/AlexKnauth/seq-no-order

> 
> I have tried:
> 
> #lang racket
> 
> (require (for-syntax syntax/parse))
> 
> (define-syntax (x stx)
> 
>  (define-syntax-class binding
>    #:description "binding list"
>    (pattern (z:id ...)))
> 
>  (define-syntax-class or-binding
>    #:description "binding or"
>    (pattern (~or zb:binding y:id)
>             #:with (z ...) #'(zb.z ...)))

This won’t work because if the y:id pattern matches instead of the zb:binding pattern, then the zb.z attribute won’t be there.
Instead you probably wan’t this:
 (define-syntax-class or-binding
   #:description "binding or"
   (pattern zb:binding
            #:with (z ...) #'(zb.z ...))
   (pattern y:id
            #:with (z ...) #'(y)) ; or whatever, depending on what you want to do here
   )

> 
>  (syntax-parse stx
>    [(_ (ob:or-binding ...) ...)
>     #''ok
>     #;#'(xf (yf ob.y ...) ...)
>     #;#'(xf (yf ob.y ... (zf ob.z ...) ...) ...)]))
> 
> (define (xf . xs) xs)
> (define (yf . ys) ys)
> (define (zf . zs) zs)
> 
> (x (a))
> (x (a b (c)))
> (x (a b c (d e f) g h))
> 
> But while the pattern “appears” to match, I can’t seem to construct a template that is acceptable to syntax-parse, which doesn’t like the #:with clause on my syntax-class or-binding. I must be missing something.
> 
> -Kevin
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140806/7aac4f01/attachment.html>

Posted on the users mailing list.