Created
April 4, 2020 14:39
-
-
Save bsless/d0cf0cc2cbd01d3871d35a3356be21a7 to your computer and use it in GitHub Desktop.
common lisp cond allows returning the predicate's value. This form does it via reader macros
This file contains 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 cond* | |
[& clauses] | |
(when clauses | |
(let [p (first clauses) | |
return? (:return (meta p))] | |
(if return? | |
`(if ~p | |
~p | |
(cond* ~@(next clauses))) | |
`(if ~p | |
~(if (next clauses) | |
(second clauses) | |
(throw (IllegalArgumentException. | |
"cond requires an even number of forms"))) | |
(cond* ~@(nnext clauses))))))) | |
;;; examples: | |
(cond* | |
p0 c0 | |
^:return p1 | |
p2 c2 | |
p3 c3) | |
(cond* | |
^:return p1 | |
p2 c2 | |
p3 c3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment