Go to the previous, next section.

The  type-of-animal Function in Detail

Let's look at the  type-of-animal function in detail.

The function definition for  type-of-animal was written by filling the slots of two templates, one for a function definition as a whole, and a second for an  if expression.

The template for every function that is not interactive is:

(defun name-of-function (argument-list)

The parts of the function that match this template look like this:

(defun type-of-animal (characteristic)
  "Print message in echo area depending on CHARACTERISTIC.  
If the CHARACTERISTIC is the symbol `fierce',
then warn of a tiger."
  body: the  if  expression)

In this case, the name of function is  type-of-animal ; it is passed the value of one argument. The argument list is followed by a multi-line documentation string. The documentation string is included in the example because it is a good habit to write documentation string for every function definition. The body of the function definition consists of the  if expression.

The template for an  if expression looks like this:

(if true-or-false-test

In the  type-of-animal function, the actual code for the  if looks like this:

(if (equal characteristic 'fierce)
    (message "It's a tiger!")))

Here, the true-or-false-test is the expression:

(equal characteristic 'fierce)

In Lisp,  equal is a function that determines whether its first argument is equal to its second argument. The second argument is the quoted symbol  'fierce and the first argument is the value of the symbol  characteristic ---in other words, the argument passed to this function.

In the first exercise of  type-of-animal , the argument  fierce is passed to  type-of-animal . Since  fierce is equal to  fierce , the expression,  (equal characteristic 'fierce) , returns a value of true. When this happens, the  if evaluates the second argument or then-part of the  if :  (message "It's tiger!") .

On the other hand, in the second exercise of  type-of-animal , the argument  zebra is passed to  type-of-animal .  zebra is not equal to  fierce , so the then-part is not evaluated and  nil is returned by the  if expression.

Go to the previous, next section.