Re: [racket-users] Macro help

2019-05-25 Thread yfzhe
Another "syntax-case-y" version generates `define-values`: (define-syntax-rule (aux clause ...) (aux-helper () (clause ...))) (define-syntax (aux-helper stx) (syntax-case stx () [(_ ([id val] ...) ()) #'(define-values (id ...) (values val ...))] [(_ (id+val ...) ([id val] more

Re: [racket-users] Macro help

2019-05-24 Thread Kevin Forchione
> On May 24, 2019, at 7:35 AM, Stephen Chang 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 >

Re: [racket-users] Macro help

2019-05-24 Thread Stephen Chang
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)

Re: [racket-users] Macro help

2019-05-24 Thread Matthias Felleisen
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))

Re: [racket-users] Macro help

2019-05-23 Thread Michael Murdock MacLeod
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

[racket-users] Macro help

2019-05-23 Thread Kevin Forchione
Hi guys, I’ve been wracking my brains all day trying to come up with a macro that would convert this syntax: ;; (aux a (b (* 2 pi)) c (d pi)) ;; => (define-values (a b c d) (values #f 6.28318530717958 #f 3.141592653589793) I’m missing some part of the picture. The closest I’ve come is to

Re: [racket-users] [macro help] How can I render a parenthesized set of elements optional?

2019-02-16 Thread David Storrs
Thank you both -- Jon for the code and Alexis for the thorough description. Alexis, this was exactly what I was looking for. I've been frustrated with macros because I kept trying and failing to figure out a mental model of what the heck was going on under the hood. I had some of the basics,

Re: [racket-users] [macro help] How can I render a parenthesized set of elements optional?

2019-02-15 Thread Alexis King
Jon is right. Here’s an explanation why. Think of `...` as a postfix operator. It repeats what comes before it a certain number of times. In order for `...` to know how many times to repeat the previous head template, it looks inside the head template for any attributes bound at the

Re: [racket-users] [macro help] How can I render a parenthesized set of elements optional?

2019-02-15 Thread Jon Zeppieri
On Fri, Feb 15, 2019 at 11:50 PM David Storrs wrote: > > #lang racket > (require (for-syntax racket/syntax syntax/parse)) > > (define-syntax (struct++ stx) > (syntax-parse stx > [(_ name:id (field:id ...) (~optional (rule:expr ...)) opt ...) > #'(begin (struct name (field ...) opt

[racket-users] [macro help] How can I render a parenthesized set of elements optional?

2019-02-15 Thread David Storrs
I'm twiddling with struct-plus-plus, adding declarative rules, and ~? isn't doing what I expect. I expect that: (~? rule) ... is equivalent to (~? rule (~@)) and means "If `rule` matched something, insert it here. Do this for each item that `rule` matched. If `rule` did not match anything,