Skip to content

Instantly share code, notes, and snippets.

@kriyative
Created July 6, 2011 18:25
Show Gist options
  • Save kriyative/1067960 to your computer and use it in GitHub Desktop.
Save kriyative/1067960 to your computer and use it in GitHub Desktop.
A lightweight `bind' macro 3
(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