Life is a Function



next up previous
Next: A Picture is Up: A Few Simple Previous: Lisp Comments

Life is a Function

Lisp makes heavy use of functions. There are zillions of them, but once you know a few, you can write very compact code. Here are some examples of the arithmetic functions:

> (+ 1 4)
5
> (- 1 4)
-3
> (* 1 4)
4
> (/ 1 4)
0.25
> (* 2 (/ 1 2))
1.0
Notice that the function is delineated by matching parenthesis and functions can be nested within functions.

For our work we need arrays of numbers. We could define them by hand as, e.g.,

> (def beach_gravity (list 22 34 30 24 55))
BEACH_GRAVITY
> beach_gravity
(22 34 30 24 55)
Fortunately many of Lisp-Stat's functions are ``vectorized'', that is, they operate on entire arrays (or vectors). Thus
> (/ beach_gravity 2)
(11 17 15 12 27.5)
results in the division of each element of beach_gravity by 2. A slightly more interesting example, would be
> (- beach_gravity (mean beach_gravity))
(-11.0 1.0 -3.0 -9.0 22.0)
Here we found the mean of the beach_gravity array, and then subtracted this element-wise from the data. Notice that we didn't have to tell the mean function how many elements there were in beach_gravity.




Tue Aug 29 09:10:30 MDT 1995