syntax-parse ellipsis question

18 views
Skip to first unread message

Jonathan Simpson

unread,
Sep 21, 2019, 4:15:47 PM9/21/19
to Racket Users
Given this macro that I'm experimenting with:

(syntax-parse #'(1 2 2 a 2 2 b 2 c) [(1 (~seq n:nat ...+ x) ...) #'((n ... x) ...)])

How would I change it so that it returns #'(2 2 a 2 2 b 2 c) instead of #'((2 2 a) (2 2 b) (2 c)) ?

I don't want the parens around the individual sequences that I'm matching, but I haven't figured out a way to do this without using a pattern that forces me to add them in the body of the syntax-parse clause. How can I group the n and x pattern variables in the body without adding parens around them?

If could get a syntax class that replaces the (~seq n:nat ...+ x) that might work but I haven't been able to get a syntax class that will do that either.

Thanks,
Jonathan

Jens Axel Søgaard

unread,
Sep 21, 2019, 4:28:36 PM9/21/19
to Jonathan Simpson, Racket Users

Two options:

(syntax-parse #'(1 2 2 a 2 2 b 2 c)
  [(1 (~seq n:nat ...+) x ...) #'(n ... x ...)])

(syntax-parse #'(1 2 2 a 2 2 b 2 c)
  [(1 n:nat ...+ x ...) #'(n ... x ...)])

/Jens Axel

Philip McGrath

unread,
Sep 21, 2019, 4:28:51 PM9/21/19
to Jonathan Simpson, Racket Users
Use `~@` (sine Racket 7.0):
(syntax-parse #'(1 2 2 a 2 2 b 2 c)
  [(1 (~seq n:nat ...+ x) ...)
   #'((~@ n ... x) ...)])

-Philip


--
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/55b38c69-a789-4528-be79-0bf48c97f283%40googlegroups.com.

Ryan Culpepper

unread,
Sep 21, 2019, 5:00:58 PM9/21/19
to Jonathan Simpson, Racket Users
On 9/21/19 10:15 PM, Jonathan Simpson wrote:
> Given this macro that I'm experimenting with:
>
> (syntax-parse #'(1 2 2 a 2 2 b 2 c) [(1 (~seq n:nat ...+ x) ...) #'((n
> ... x) ...)])
>
> How would I change it so that it returns #'(2 2 a 2 2 b 2 c) instead of
> #'((2 2 a) (2 2 b) (2 c)) ?
>
> I don't want the parens around the individual sequences that I'm
> matching, but I haven't figured out a way to do this without using a
> pattern that forces me to add them in the body of the syntax-parse
> clause. How can I group the n and x pattern variables in the body
> without adding parens around them?

Use `~@` in the template:

#'((~@ n ... x) ...)

Ryan

Jonathan Simpson

unread,
Sep 21, 2019, 5:08:30 PM9/21/19
to Philip McGrath, Racket Users
Thanks. I'm currently running 6.12, so this may be what convinces me to go ahead and upgrade.

-- Jonathan
Reply all
Reply to author
Forward
0 new messages