[racket-users] macro guard question (ellipsis)

38 views
Skip to first unread message

Kevin Forchione

unread,
Jun 18, 2019, 11:11:00 PM6/18/19
to Racket-Users List
Hi guys,
I’ve been trying to figure out the syntax for a guard when the pattern has ellipsis and you want to guard one of the variables. Here’s an uninteresting macro but it shows what I mean.

#lang racket

(define-syntax (foo stx)
(syntax-case stx ()
[(_ (key val ...) ...)
(and (identifier? #'key) (identifier? #'val))
#'(quote (list ((quote key) (quote val) ...) ...))]))

(foo (a b) (c d e))

Of course it complains about the ellipsis being missing in the pattern. Is this something I can handle with a guard in this manner or do I need a helper macro of some sort?

Thanks!
Kevin


Sorawee Porncharoenwase

unread,
Jun 18, 2019, 11:56:50 PM6/18/19
to Kevin Forchione, Racket-Users List

I’d write this. Not sure if there’s a better way.

(define-syntax (foo stx)
  (syntax-case stx ()
    [(_ (key val ...) ...)
     (andmap identifier? (syntax->list #'(key ... {~@ val ...} ...)))
     #'(quote (list ((quote key) (quote val) ...) ...))]))

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/D76D1CBC-BF78-493F-89B9-9498D615A5E7%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Sorawee Porncharoenwase

unread,
Jun 19, 2019, 12:03:45 AM6/19/19
to Kevin Forchione, Racket-Users List

Also note that with syntax/parse, it becomes much easier:

(require (for-syntax syntax/parse))

(define-syntax (foo stx)
  (syntax-parse stx
    [(_ (key:id val:id ...) ...)
     #'(quote (list ((quote key) (quote val) ...) ...))]))

Kevin Forchione

unread,
Jun 19, 2019, 12:24:00 PM6/19/19
to Sorawee Porncharoenwase, Racket-Users List


On Jun 18, 2019, at 9:03 PM, Sorawee Porncharoenwase <sorawe...@gmail.com> wrote:

Also note that with syntax/parse, it becomes much easier:

(require (for-syntax syntax/parse))

(define-syntax (foo stx)
  (syntax-parse stx
    [(_ (key:id val:id ...) ...)
     #'(quote (list ((quote key) (quote val) ...) ...))]))

Thanks! Looks like it’s finally time for me to do a deep dive into syntax-parse. 

Kevin
Reply all
Reply to author
Forward
0 new messages