Conditionals and Equality
7.1 Conditionals
(if (> 3 2) "yes" "no") ; "yes"
(if nil "yes") ; NIL (missing else → nil)
(when (> 3 2) ; multiple forms, no else
(print "three is bigger")
:done)
(unless (> 1 2) (print "1 is not bigger"))
(defun classify (n)
(cond ((< n 0) "negative")
((= n 0) "zero")
((< n 10) "small")
(t "large"))) ; t = otherwise
(defun day-type (day)
(case day ; compares with eql
((:sat :sun) "weekend")
((:mon :tue :wed :thu :fri) "weekday")
(otherwise "unknown")))
(defun describe-thing (x)
(typecase x
(integer "an integer")
(string "a string")
(list "a list")
(t "something else")))
(ecase :mon (:mon 1) (:tue 2)) ; ecase/etypecase signal errors when nothing matches
(and 1 2 3) ; 3 (returns the last true value)
(or nil 2 3) ; 2 (returns the first true value)
(not nil) ; T7.2 Equality Predicates
Symbols, the same object (not numbers/chars) eq, plus numbers of the same type and characters
The default for most functions (member, case, hash tables)
Structural: conses, strings (case-sensitive), bit vectors, pathnames Lists and strings
Like equal, but case-insensitive strings, numbers with =, arrays and hash tables element-wise Loose structural comparison Numbers ((= 1 1.0) → T) string= / string-equal Strings (case-sensitive / insensitive) char= / char-equal
(eq 'a 'a) ; T
(eql 1 1) ; T
(eql 1 1.0) ; NIL (different types)
(= 1 1.0) ; T
(equal "abc" "abc") ; T
(eq "abc" "abc") ; NIL (probably different objects)
(equalp "ABC" "abc") ; T
(equal '(1 (2)) '(1 (2))) ; T
(member "b" '("a" "b") :test #'equal) ; ("b"): use :test for non-eql comparisons