Skip to content

Instantly share code, notes, and snippets.

@Wilfred
Created June 12, 2012 14:09
Show Gist options
  • Save Wilfred/2917761 to your computer and use it in GitHub Desktop.
Save Wilfred/2917761 to your computer and use it in GitHub Desktop.
and-let* from Scheme implemented in Emacs Lisp
(eval-when-compile (require 'cl)); declare, first, rest
;;;; and-let*, based on Scheme's SRFI 2
;; Contrived usage example:
;; (and-let* ((buffer-path (buffer-file-name))
;; (file-name (file-name-directory)))
;; (message "You are visiting a file called %s in this buffer."
;; file-name))
(defmacro and-let* (varlist &rest body)
"Equivalent to let*, but terminates early and returns nil if
any variable is bound to nil. BODY is only evaluated if all
variables were non-nil."
(declare (debug (form &rest form)))
(if (eq varlist '()) `(progn ,@body)
(let* ((first-binding (first varlist))
(var-name (first first-binding)))
`(let (,first-binding)
(if ,var-name
(and-let* ,(rest varlist) ,@body))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment