Skip to content

Instantly share code, notes, and snippets.

@vyzo
Created December 19, 2017 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vyzo/51656b672c3d98cd21c8235b53dcf811 to your computer and use it in GitHub Desktop.
Save vyzo/51656b672c3d98cd21c8235b53dcf811 to your computer and use it in GitHub Desktop.
let-alist
(defsyntax (let-alist stx)
(syntax-case stx ()
((macro expr body ...)
(with-syntax ((alist-ref (stx-identifier #'macro '%%ref)))
#'(let (alist expr)
(let-syntax
((alist-ref
(lambda (stx)
(syntax-case stx ()
((_ id)
(let (str (symbol->string (stx-e #'id)))
(if (eq? (string-ref str 0) #\.)
(let (sym (string->symbol (substring str 1 (string-length str))))
(with-syntax ((alist (quote-syntax alist))
(elem sym))
#'(cond
((assq 'elem alist) => cdr)
(else
(error "Missing alist element" alist 'elem)))))
#'(%#ref id))))))))
body ...))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment