Skip to content

Instantly share code, notes, and snippets.

@fare
Created August 22, 2017 01:25
Show Gist options
  • Save fare/f52184d070b54b137fe5da09bd0471f1 to your computer and use it in GitHub Desktop.
Save fare/f52184d070b54b137fe5da09bd0471f1 to your computer and use it in GitHub Desktop.
Trying to nest a helper inside a macro...
#lang racket
(define-syntax defsyntax (syntax-rules () ((_ x ...) (define-syntax x ...))))
;; These two work:
(defsyntax Rnest
(syntax-rules ()
((_ (rev ...) (one more ...)) (Rnest (one rev ...) (more ...)))
((_ () ()) ())
((_ (x) ()) x)
((_ (x (y ...) z ...) ()) (Rnest ((y ... x) z ...) ()))))
(defsyntax nest
(syntax-rules ()
((_ x ...) (Rnest () (x ...)))))
(write (nest (quote) (a b) (c d) (e) f)) (newline) ;;=> prints: (a b (c d (e f)))
;; I tried to make Rnest a local macro, but failed with an error as follows
;; the position given being after the first z:
;; syntax: no pattern variables before ellipsis in template
(defsyntax nest
(syntax-rules ()
((_ x ...)
(let-syntax
((r
(syntax-rules ()
((_ (x ...) (y z ...)) (r (y x ...) (z ...))) ;; reverse the list
((_ (x (y ...) z ...) ()) (r ((y ... x) z ...) ())))) ;; nest
((_ (x) ()) x)) ;; bottom case
(r () (x ...))))))
;; I tried unhoisting the let-syntax, and only got this errors in the last r with: r: unbound identifier in module
(defsyntax nest
(let-syntax
((r
(syntax-rules ()
((_ (x ...) (y z ...)) (r (y x ...) (z ...))) ;; reverse the list
((_ (x (y ...) z ...) ()) (r ((y ... x) z ...) ())) ;; nest
((_ (x) ()) x)))) ;; bottom case
(syntax-rules ()
((_ x ...) (r () (x ...))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment