PREDICATES AND CONDITIONALS

A predicate is a procedure that returns true or false.

EQUAL, EQL, EQ, and = are equality predicates

[1]> (= 1 1) ;; with =, the numbers do not have to be the same type

T

[2]> (eq 1 1) ;; with =, the numbers do have to be the same type

T

[3]> (eql 1 1) ;; also (equal 1 1) is T but (equal 1 1.0) is NIL

T

[4]> (= 1 1.0) ;; = is used for numbers

T

[5]> (eql 1 1.0) ;; also: (eq 1 1.0) is NIL the numbers have to be the same type

NIL

[6]> (eq 'a 'a) ;; eq checks if the values are the same symbol

T

[7]> (setf a 'hello)

HELLO

[8]> (eq a 'hello)

T

[9]> (eql a 'hello)

T

[10]> (setf a '(hello))

(HELLO)

[11]> (eq a '(hello))

NIL

[12]> (eql a '(hello))

NIL

[13]> (equal a '(hello)) ;; equal is more generalized

T


MEMBER tests for list membership

>(setf sentence '(tell me more about your mother please))

>(member 'mother sentence)

(MOTHER PLEASE)


>(setf pairs '((father son) (mother daughter)))

>(member 'mother pairs)

NIL


LISTP, ATOM, NUMBERP, and SYMBOLP are data-type predicates

>(atom 'pi)

T

>(atom pi)

T

>(numberp 'pi)

NIL


>(numberp pi)

T

>(symbolp 'pi)

T

>(symbolp pi)

NIL

>(listp 'pi)

NIL

>(listp '(this a list with pi in it))

T


Expression

List Atom

Cons Empty list Symbol Number

Float, Ratio, Integer



NIL is equivalent to the empty list:

>(eq nil '())

T

>(atom nil)

T

>(atom ())

T

>(symbolp nil)

T

>(symbolp ())

T

>(listp nil)

T

>(listp ())

T


NULL and ENDP are empty list predicates

>(null '(this is not empty))

NIL

>(endp '(this is not empty))

NIL

>(null ())

T

>(endp ())

T

>(null 'this-symbol)

NIL

>(endp 'this-symbol)

ERROR ;; endp must have a list as a parameter


There are many number predicates

numberp, zerop, plusp, minusp, evenp, oddp, > , <


AND, OR, and NOT

>(setf pets '(dog cat))

>(and (member 'dog pets) (member 'tiger pets))

NIL

>(or (member 'dog pets) (member 'tiger pets))

(DOG CAT)

>(and (member 'dog pets) (member 'cat pets))

(CAT)


OR and AND return the last value compute, not just a T or NIL.

In this case, the last value computed was the part of the list returned when OR or AND was true.


>(not nil)

T

>(not t)

NIL

>(not (member 'dingo pets))

T


IF, UNLESS, and WHEN

(if <test> <then form> <else form>)


>(setf day-or-date 'monday)

>(if (symbolp day-or-date) 'day 'date)

DAY

>(setf day-or-date 9)

>(if (symbolp day-or-date) 'day 'date)

DATE

(if <test> <then form> nil) = (when <test> <then form>)

(if <test> nil <else form>) = (unless <test> <else form>)

WHEN and UNLESS can have an unlimited number of arguments


COND

>(setf thing 'sphere r 1)

>(cond ((eq thing 'circle) (* pi r r))

((eq thing 'sphere) (* 4 pi r r)))

12.56637

>(setf p .6)

(cond ((> p .75) 'very-likely)

((> p .5) 'likely)

((> p .25) 'unlikely)

(t 'very-unlikely))

LIKELY

CASE

>(case thing ;; "thing" is the symbol to be evaluated

(circle (* pi r r)) ;; is "thing" equal to "circle"?

(sphere (* pi r r))) ;; is "thing" equal to "sphere"?


CASE can also use "otherwise",

>(setf thing 'point r 1)

>(case thing

(circle (* pi r r))

(sphere (* 4 pi r r))

(otherwise 0))

0


CASE checks for membership in a list:

>(setf thing 'ball r 1)

>(case thing

((circle wheel) (* pi r r))

((sphere ball) (* 4 pi r r))

(otherwise 0))

12.56637


USING COND and CASE in function definitions

(defun express-probability (p)

(cond ((> p .75) 'very-likely)

((> p .5) 'likely)

((> p .25) 'unlikely)

(t 'very-unlikely)))


>(express-probability .8)

VERY-LIKELY

>(express-probability .6)

LIKELY

>(express-probability .4)

UNLIKELY

>(express-probability .2)

VERY-UNLIKELY


(defun both-ends (whole-list)

(case (length whole-list NIL)

(0 NULL)
(1 (cons (first whole-list)

      whole-list))

(2 whole-list)

(t (cons (first whole-list)

(last whole-list)))))