Slide 8.b: Definitions and basic functions
Slide 8.d: COND
Home

Basic Functions (Cont.)


In LISP, a function call is written as a list. The CAR (first element) of the list is the name of the function to be called, and CDR (rest of the elements) is a list of S-expressions to be evaluated before being passed to the function (except in a few special cases, such as QUOTE and DEFUN). For example, the following expression returns as value a new list which is like '(Dogs are animals.) except that its first element has been replaced by 'Cats.

  (cons  '  (cdr  ' ))
   

       


Parentheses can not be used freely in LISP as in other programming languages; every parenthesis has a meaning. In particular, an atom immediately following an open parenthesis is treated as the name of a function. To test if two atoms are equal in value you may write (EQ A1 A2), but if you accidentally write (EQ (A1 A2)), this says to call the function A1 with the parameter A2, then use the result as the single parameter to EQ. The following expression creates a new list including two sub-lists by using the instructions LIST and SETQ.

  (list  (setq  lst '  lst))
   

       


Some LISP implementations are case-sensitive; they may require that predefined names such as CONS be capitalized. Some implementations may require that they be lowercase, e.g. cons. Some implementations ignore case. Some implementations just treat everything as uppercase, regardless of how you type it. You may use whitespace (blanks, tabs, and newlines) to format LISP programs however you please.