Created
February 23, 2013 20:23
-
-
Save dyoo/5021197 to your computer and use it in GitHub Desktop.
A syntax parse example for parsing subterms in arbitrary order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require (for-syntax syntax/parse)) | |
(struct person (name age) #:transparent) | |
;; Accept name and age in any order: | |
(define-syntax (new-person stx) | |
(syntax-parse stx | |
[(_ (~or (~optional ((~datum name) name) #:defaults ([name #'"Jane Doe"])) | |
(~optional ((~datum age) age) #:defaults ([age #'#f]))) | |
...) | |
#'(person name age)])) | |
(new-person (age 34)) | |
(new-person (name "Pen Pen")) | |
(new-person (age 48) (name "Gendo Ikari")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment