Conditionals

  • The basic macro to know: if
(if test
    (progn
    then
    continuing)
    else)
  • If you don't need an else clause, then you should use when instead. It also has an implicit progn.
(when test
    then
    continuing)
  • When you want something like when (not test), use unless. It is the contrary of when.
(unless test 
    then
    continuing)
  • At some point, when you have some imbricated ifs, you can use cond.
(cond 
    (test
    logic
    logic2)
    (test 2
    logic)
    (t :default))
  • There is also case.
(let ((a 1))
  (case a
    (1 :its1)
    (t :default)))
  • There is another feature that helps you do conditionals, but a bit higher level, it's the *features* variables.
(push :udemy *features*)

(defun test ()
  #+unix 
  (print "we are on Linux"))
  • If you use something that cannot be truthy, like NIL:
#+nil (assert (= 2 (+ 1 1)))
  • and and or can be used as shortcuts for conditionals.
(if :truthy
    :truthy
    :falsy)

;; can be shortened to:

(or :truthy
    :falsy)