[racket] macro pattern problem

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Jan 26 17:25:44 EST 2015

1. You should make your macro robust and deal with fewer than two elements in your uses. 

2. Here is the same idea w/ syntax-rules: 

#lang racket

(define-syntax quote-even 
  (syntax-rules ()
    [(_) '()]
    [(_ zero) (list 'zero)]
    [(_ zero one two ...) (list* 'zero one (quote-even two ...))]))

(equal? (quote-even) '())
(equal? (quote-even a) '(a))
(equal? (quote-even a (+ 10 1) b (+ 20 2) c (+ 30 3) d) '(a 11 b 22 c 33 d))


On Jan 26, 2015, at 4:57 PM, "Alexander D. Knauth" <alexander at knauth.org> wrote:

> syntax/parse can do that:
> #lang racket
> (require (for-syntax racket/base syntax/parse syntax/parse/experimental/template))
> (define-syntax quote-even
> (syntax-parser
>   [(quote-even (~seq att val) ...)
>    (template (list (?@ 'att val) ...))]))
> (quote-even a 1 b 2) ; '(a 1 b 2)
> 
> 
> On Jan 26, 2015, at 4:48 PM, Peter Samarin <petrsamarin at gmail.com> wrote:
> 
>> Hello all,
>> 
>> I want to quote every even and evaluate every odd variable of the input and assemble everything in a list.
>> So I wrote the following macro to do it:
>> 
>> (define-syntax quote-even
>> (syntax-rules ()
>>   [(quote-even att val ...)
>>    (list 'att val ...)]))
>> 
>> But in the resulting list, only the very first attribute is quoted:
>> 
>> (syntax->datum
>> (expand-once
>> '(quote-even a 10 b 20 c 30)))
>> 
>>> (list 'a 10 b 20 c 30)
>> 
>> (quote-even a 10 b 20 c 30)
>>> b: undefined;
>> 
>> 
>> Is there a way to do do it?
>> 
>> Cheers,
>> Peter Samarin
>> ____________________
>> Racket Users list:
>> http://lists.racket-lang.org/users
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



Posted on the users mailing list.