[racket] syntax-parse question

From: Kevin Forchione (lysseus at gmail.com)
Date: Thu Aug 7 01:41:49 EDT 2014

On Aug 6, 2014, at 11:02 AM, Alexander D. Knauth <alexander at knauth.org> wrote:

> 
> On Aug 6, 2014, at 1:47 PM, Alexander D. Knauth <alexander at knauth.org> wrote:
> 
>> 
>> 
>> 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 ...)))])
> 
> Sorry I forgot an ellipsis.  I meant this:
> (syntax-parse stx
>   [(_ (~or (z ...)
>            y)
>       ...)
>    #'(xf (yf y ... (zf z ...) ...))])


Remarkably difficult just to parse what amounts to nested lists! This doesn’t quite do what I want either. For instance:

#lang racket

(require (for-syntax syntax/parse))

#;(define-syntax (x stx)
  
  (define-syntax-class binding
    #:description "binding list"
    (pattern (z:number ...)))
  
  (syntax-parse stx
    [(_ b:binding ...)
     #'(list (list b.z ...) ...)]))

#;(define-syntax (x stx)
  
  (define-syntax-class lbinding
    #:description "binding list"
    (pattern (z:number ...)))
  
  (define-syntax-class orbinding
    #:description "binding or"
    (pattern (y:number ...)
             #:with (z ...) #'(y ...))
    (pattern (lb:lbinding)
             #:with (z ...) #'(lb.z ...)))
  
  (syntax-parse stx
    [(_ ob:orbinding ...)
     #'(list (list ob.z ...) ...)]))

#;(define-syntax (x stx)
  
  (define-syntax-class lbinding
    #:description "binding list"
    (pattern (z:number ...)))
  
  (define-syntax-class orbinding
    #:description "binding or"
    (pattern (y:number ...)
             #:with (z ...) #'(y ...))
    (pattern (lb:lbinding)
             #:with (z ...) #'(lb.z ...)))
  
  (syntax-parse stx
    [(_ ob:orbinding ...)
     #'(list (list ob.z ...) ...)]))

(define-syntax (x stx)
  (syntax-parse stx
    [(_ (~or (z ...)
             y)
        ...)
     #'(list (list y ... (list z ...) ...))]))

(x (1 2 3 4) (5 (6 7) 8))

expands to:  (list (list (list 1 2 3 4) (list 5 (6 7) 8))))) instead of  (list (list (list 1 2 3 4) (list 5 (list 6 7) 8))))). 

-Kevin

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

Posted on the users mailing list.