Conditionals

June 8, 2008 | Filed Under Common Lisp - Touretzky 

———————————————————
Chapter 4
———————————————————

Conditionals - IF:

(IF (ODDP 5) ‘odd ‘even) -> odd

COND

(DEFUN COMPARE-TO (x y)
(COND ((EQUAL x y) ‘equal)
((> x y) ‘x-greater-than-y)
((

(DEFUN WHICH-COUNTRY (x)
(COND ((EQUAL x 'LONDON) 'ENGLAND)
((EQUAL x 'PARIS) 'FRANCE)
(T 'UNKNOWN))) ;default fall through

AND and OR macros:

(AND (> 2 1) (< 3 4)) ->T
(OR (> 1 2) (< 3 4)) -> T

AND progresses until it hits a NIL (in which case it returns NIL) or the end of the arguments, in which case it returns the evaluated last argument.

OR progresses until it hit a non-NIL argument (in which case it returns that evaluated argument) or the end of the arguments in which case it returns NIL.

(AND ‘a ‘b ‘c) -> c
(AND ‘(1 2) ‘(3 4)) -> (3 4)

(PLUSP 4) -> T
(PLUSP -4) -> NIL

Boolean functions:
(DEFUN LOGICAL-AND (x y)
(AND (x y T)))

If both x and y and non-NIL, then the function returns the last evaluated argument (in this case, T)

DeMorgan’s Theorum

(and x y) = (not (or (not x) (not y)))
(or x y) = (not (and (not x) (not y)))

Comments

Leave a Reply