Created
July 6, 2011 18:25
-
-
Save kriyative/1067960 to your computer and use it in GitHub Desktop.
A lightweight `bind' macro 3
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
(defmacro bind (clauses &body body) | |
"This macro combines the behaviour of the forms `let*', `destructuring-bind', | |
and `multiple-value-bind', permitting the following style of binding form: | |
(bind (((:values m n) (values 10 20)) | |
((a b &key (c 10)) '(1 2)) | |
(x 5)) | |
(+ x a b c m n)) | |
=> 48 | |
This is a more limited and lightweight implementation of some ideas from | |
metabang-bind (http://common-lisp.net/project/metabang-bind/)." | |
(cond | |
((null clauses) `(progn ,@body)) | |
((and (listp (caar clauses)) (eq (caaar clauses) :values)) | |
`(multiple-value-bind ,(cdaar clauses) | |
,@(cdar clauses) | |
(bind ,(cdr clauses) ,@body))) | |
((listp (caar clauses)) | |
`(destructuring-bind ,(caar clauses) | |
,@(cdar clauses) | |
(bind ,(cdr clauses) ,@body))) | |
(t | |
`(let (,(car clauses)) | |
(bind ,(cdr clauses) ,@body))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment