next up previous
Contents Next: Exercises Up: More Predicates and Previous: Checking for NIL

Logical Operators: And and Or

And and or are functions but not predicates since they may return values other than t or nil. Each evaluates its arguments in the order they appear, but only enough arguments to provide a definitive result are evaluated. So, some arguments to and and to or may not be evaluated at all.

And returns nil as soon as it finds an argument which evaluates to nil; otherwise it returns the value of its last argument. For example:

>(and 1 2 3 4)
4

>(and 1 (cons 'a '(b)) (rest '(a)) (setf y 'hello))
NIL
In the example immediately above, the expression (setf y 'hello) is never evaluated since (rest '(a)) returns nil. You can check this out by evaluating y directly:
>y
27
Or returns the result of its first non-nil argument, and does not evaluate the rest of its arguments. If all the arguments evaluate to nil, then or returns nil. Examples:
>(or nil nil 2 (setf y 'goodbye))
2

>(or (rest '(a)) (equal 3 4))
NIL
Once again, you will see that y's value is unchanged by these examples.



© Colin Allen & Maneesh Dhagat
November 1999