Skip to content

Instantly share code, notes, and snippets.

@cvmat
Created February 2, 2012 04:10
Show Gist options
  • Save cvmat/1721391 to your computer and use it in GitHub Desktop.
Save cvmat/1721391 to your computer and use it in GitHub Desktop.
and-let* for Emacs LISP
(defmacro and-let* (bindings &rest body)
(let ((head (car bindings))
(rest (cdr bindings)))
(cond
((null head)
`(progn
,@body))
((and (null rest) (null body))
(cond
((symbolp head)
head)
((symbolp (car head))
(let ((exp (cadr head)))
exp))
((listp (car head))
(let ((exp (car head)))
exp))))
((symbolp head)
`(if ,head
(and-let* ,rest ,@body)
nil))
((symbolp (car head))
(let ((var (car head))
(exp (cadr head))
(tmp-var (gensym)))
`(let ((,tmp-var ,exp))
(if ,tmp-var
(let ((,var ,tmp-var))
,(if (and (null rest) (null body))
var
`(and-let* ,rest ,@body)))
nil))))
((listp (car head))
(let ((exp (car head)))
`(if ,exp
(and-let* ,rest ,@body)
nil)))
(t
nil))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment