.comment -*- Mode:TEXT; -*- .document COND - Predicates and conditionals. .tag COND Lesson COND, Version 2 Modified by Victoria Pigman, 9/1/82 In order for a program to do anything useful it must be able to do one thing one time and one thing another. In Lisp this is done with the same functional style as everything else. That is to say, doing something conditionally, and testing conditions, are both done with the use of functions. The primary function for conditional execution is the function COND. COND is a magic function in that it doesn't evaluate its arguments normally. We shall call these functions special forms because they behave differently than normal forms. Before we can start using COND however, we need some tests. The first test we shall discuss is called EQUAL, and its purpose is probably pretty obvious. Its function is not so obvious, however, so let's try a couple of examples. .eval (setq foo '((a b) c d e (f g)) bar foo dog 'animal cat 'animal geranium 'plant violet 'plant fadip nil dore nil man t god t pigs 7 figs 7 monkeys 8 thing1 'dog thing2 'cat thing3 'violet thing4 'monkey thing5 'man thing6 'geranium thing7 'pigs) The following atoms have been given values for you: FOO BAR DOG CAT GERANIUM VIOLET FADIP DORE MAN GOD PIGS FIGS MONKEYS THING1 THING2 THING3 THING4 THING5 THING6 THING7 Find their values, and then use them (or your own) to find out which are EQUAL. That is, apply the function EQUAL to pairs of them until you figure out the pattern. I'll give you a hint... T means true, and NIL means false in Lisp. For example: (equal dog dog) will return T. .try As you saw, EQUAL things are things which print out the same. In time, we will be able to more precisely define EQUAL in terms of an even more primitive function, EQ. However, we are not yet prepared to deal with that. And now let us see how we can use predicates to choose what we want to do. The special form COND is a way to take one of several actions depending on the truth/falsity of several predicates. The description of T above as meaning TRUE is a little misleading. In fact, ANY non-NIL can and will be interpreted as true. T is special in that it always evaluates to itself and cannot be modified. COND is a special form which takes 1 to infinity of arguments. Each argument, or clause, is of the form "( ...)" where is something which is evaluated to see if it is true or false (non-NIL or NIL), and the FORM-I's are optional forms to be evaluated. Each of these clauses has its predicate evaluated in turn until one of them evaluates non-NIL. If the predicate is nil, the forms following it in the clause are not evaluated. However, if the predicate is non-nil, then the rest of its forms are evaluated in turn, and the last one is returned as the value of the COND. The remainder of the COND is not examined at all. If no predicate is true, the value of the COND is NIL. A few examples will help clarify: .pp (COND ((EQUAL thing 'dog) 'animal) ((EQUAL thing 'geranium) 'plant) (t 'dont-know)) Will return ANIMAL if THING is has the value DOG, PLANT if it has the value GERANIUM, and otherwise it will return DONT-KNOW. Note that the T in the last clause is always true. Hence if nothing else is true, the last clause will be evaluated. This corresponds to ELSE in many other languages. You should now try experimenting with COND. .try .next FIB