next up previous
Contents Next: Functions with Extended Up: Defining Lisp functions Previous: Using an Editor

Using Your Own Definitions in New Functions

User-defined functions can be included in new function definitions. For example:

> (defun fourth-power (x)
   (square (square x)))
FOURTH-POWER

> (fourth-power 2)
16
It is worth paying attention to what happens when fourth-power is called. During the computation, the variable ``x'' gets assigned as many as four times. First, x is bound to 2, which is its local value for the function fourth-power. Fourth-power evaluates (square (square 2)), which means that square is called once with the argument 2, and again with the argument 4. Each time square is called, a local version of x is bound to the appropriate value. Finally, x is restored to whatever global value it previously had.



© Colin Allen & Maneesh Dhagat
March 2007