> On May 24, 2019, at 7:35 AM, Stephen Chang <stch...@ccs.neu.edu> wrote:
> 
> If `define-values` is not strictly required, here's a more
> syntax-case-y recursive version:
> 
> (define-syntax (aux stx)
>  (syntax-case stx ()
>    [(_) #'(begin)]
>    [(_ (var val) . rst)
>     (identifier? #'var)
>     #'(begin
>         (define var val)
>         (aux . rst))]
>    [(_ var . rst)
>     (identifier? #'var)
>     #'(begin
>         (define var #f)
>         (aux . rst))]))
> 
> On Fri, May 24, 2019 at 9:33 AM Matthias Felleisen
> <matth...@felleisen.org> wrote:
>> 
>> 
>> Let me propose the use of syntax-parse as an alternative here. I think it 
>> clarifies the purpose of the maco. — Matthias
>> 
>> 
>> #lang racket
>> 
>> (require (for-syntax syntax/parse))
>> 
>> #; (aux a (b (* 2 pi)) c (d pi))
>> ;; =>
>> #; (define-values (a b c d) (values #f 6.28318530717958 #f 
>> 3.141592653589793))
>> 
>> 
>> (begin-for-syntax
>>  (define-syntax-class optionally-initiliazed
>>    (pattern (lhs:id rhs:expr))
>>    (pattern lhs:id #:attr rhs #'#f)))
>> 
>> (define-syntax (aux stx)
>>  (syntax-parse stx
>>    [(_ p:optionally-initiliazed ...) #'(define-values (p.lhs ...) (values 
>> p.rhs ...))]))
>> 
>> (aux a (b (* 2 pi)) c (d pi))
>> 
>> 
>> 
>> 
>> On May 24, 2019, at 12:52 AM, Michael Murdock MacLeod 
>> <michaelmmacl...@gmail.com> wrote:
>> 
>> Does this work? It uses a helper function, `prune`, to parse the var-val
>> clauses.
>> 
>> #lang racket
>> 
>> (define-for-syntax (prune stx)
>> (syntax-case stx ()
>>   [()
>>    #'()]
>>   [((var val) others ...)
>>    (cons #'(var val)
>>          (prune #'(others ...)))]
>>   [(var others ...)
>>    (cons #'(var #f)
>>          (prune #'(others ...)))]))
>> 
>> (define-syntax (aux stx)
>> (syntax-case stx ()
>>   [(_ terms ...)
>>    (with-syntax ([((var val) ...) (prune #'(terms ...))])
>>      #'(define-values (var ...) (values val ...)))]))
>> 
>> (aux a (b (* 2 pi)) c (d pi))
>> a
>> b
>> c
>> d
>> 
>> ;; output shown below
>> 
>> #f
>> 6.283185307179586
>> #f
>> 3.141592653589793

Wow! Thanks so much guys. Each of these solutions adds another brick to the 
wall of my macro eduction!

Kevin

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/88FB1562-1F78-434E-8D6A-78DE8D86C909%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to