Skip to content

Instantly share code, notes, and snippets.

@zeptometer
Last active August 29, 2015 13:56
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 zeptometer/8885216 to your computer and use it in GitHub Desktop.
Save zeptometer/8885216 to your computer and use it in GitHub Desktop.
(import
(scheme base)
(scheme write)
(scheme cxr)
(picrin macro))
(define-syntax destructuring-bind
(ir-macro-transformer
(lambda (form inject compare)
(let ((pattern (cadr form))
(value (caddr form))
(body (cdddr form)))
(cond ((null? pattern)
`(begin ,@body))
((symbol? pattern)
`(let ((,pattern ,value)) (begin ,@body)))
((pair? pattern)
`(let ((evaluated ,value))
(destructuring-bind ,(car pattern) (car evaluated)
(destructuring-bind ,(cdr pattern) (cdr evaluated)
,@body)))))))))
(destructuring-bind (a b . c) '((1 2) 3 4 5) (list a b c)) ; ok
(destructuring-bind ((a b) . c) '((1 2) 3 4 5) (list a b c)) ; fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment