Macro WHEN, UNLESS
when test-form form* => result*
unless test-form form* => result*
test-form - フォーム
form - 暗黙のprogn
result - test-formがtrueのときは whenフォーム内のformの多値。 test-formがfalseのときは unlessフォーム内のformの多値。 それ以外はnil。
whenとunlessは、 test-form単体に依存したフォームの実行を行います。
whenのフォーム内において、 test-formがtrueのときは、formが左から右の順番で評価され、 そのformの多値がwhenフォームからの返却値になります。 一方、test-formがfalseのときは、formは評価されず whenフォームはnilを返却します。
unlessのフォーム内において、 test-formがfalseのときは、formが左から右の順番で評価され、 そのformの多値がunlessフォームからの返却値になります。 一方、test-formがtrueのときは、formは評価されず unlessフォームはnilを返却します。
(when t 'hello) => HELLO
(unless t 'hello) => NIL
(when nil 'hello) => NIL
(unless nil 'hello) => HELLO
(when t) => NIL
(unless nil) => NIL
(when t (prin1 1) (prin1 2) (prin1 3))
>> 123
=> 3
(unless t (prin1 1) (prin1 2) (prin1 3)) => NIL
(when nil (prin1 1) (prin1 2) (prin1 3)) => NIL
(unless nil (prin1 1) (prin1 2) (prin1 3))
>> 123
=> 3
(let ((x 3))
(list (when (oddp x) (incf x) (list x))
(when (oddp x) (incf x) (list x))
(unless (oddp x) (incf x) (list x))
(unless (oddp x) (incf x) (list x))
(if (oddp x) (incf x) (list x))
(if (oddp x) (incf x) (list x))
(if (not (oddp x)) (incf x) (list x))
(if (not (oddp x)) (incf x) (list x))))
=> ((4) NIL (5) NIL 6 (6) 7 (7))なし。
なし。
なし。
(when test {form}+) == (and test (progn {form}+))
(when test {form}+) == (cond (test {form}+))
(when test {form}+) == (if test (progn {form}+) nil)
(when test {form}+) == (unless (not test) {form}+)
(unless test {form}+) == (cond ((not test) {form}+))
(unless test {form}+) == (if test nil (progn {form}+))
(unless test {form}+) == (when (not test) {form}+)