Skip to content

Instantly share code, notes, and snippets.

@vyzo
Created December 21, 2017 17:04
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/a7bfde350f417692f3c032b14513d413 to your computer and use it in GitHub Desktop.
Save vyzo/a7bfde350f417692f3c032b14513d413 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
((var-ref
(syntax-rules ()
((_ id) (%%ref id)))))
(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 ((elem sym))
#'(cond
((assq 'elem alist) => cdr)
(else
(error "Missing alist element" alist 'elem)))))
#'(var-ref id))))))))
body ...)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment