Skip to content

Latest commit

 

History

History
67 lines (57 loc) · 1.68 KB

conditionals.md

File metadata and controls

67 lines (57 loc) · 1.68 KB

Conditionals

(if (evenp 7)
    (format t "7 is even~%")
    (format t "7 is odd~%"))
;; => 7 is odd
;;
;;
;;    NIL
  • p at the end of evenp function indicates that it is a "predicate" (i.e. returns t or nil).
  • There are no then or else - the second expression is evaluated if the condition is not nil, the third is evaluated if it is nil.
(when (= 0 (mod 8 4))
  (format t "8 is divisible by 4~%")
  (princ "Not, but really!"))
;; => 8 is divisible by 4
;;    Not, but really!
;;
;;    "Not, but really!"
  • when lets you run multiple expressions given the condition is not nil
  • if would be perfectly valid for a single expression but when is more idiomatic
(cond ((< 9 0)
       (format t "9 is negative"))
      ((< 9 10)
       (format t "9 has 1 digit"))
      (t
       (format t "9 has multiple digits")))
;; => 9 has 1 digit
;;
;;    NIL
  • Multiple conditions can be written with cond. The first condition that is not nil will be evaluated and returned.
  • 'Else', as implemented in other languages can be written as the symbol t which always evaluates to t, which is not nil.
(defun what-am-i (i)
  (cond ((stringp i)
         "I am a string!!")
        ((integerp i)
         "I am an integer!!")
        ((listp i)
         "I am a list!!")))
;; => WHAT-AM-I

(what-am-i 1)
;; => "I am an integer!!"
(what-am-i "blah-blah")
;; => "I am a string!!"
(what-am-i (list 1 2 3))
;; => "I am a list!!"
(what-am-i 1/2)
;; => NIL
  • cond is an expression
  • There are built-in functions to test the types of variables. As expected (haha) they end in "p"
  • If none of the conditions match, nil is returned