Next: Recursion, Previous: Truth, Up: Welcome to Lisp [Contents][Index]
You can define new functions with defun
. It usually takes three or more
arguments:
Symbols are variable names, existing as objects in their own right. That’s why symbols, like lists, have to be quoted. A list has to be quoted because otherwise it will be treated as code; a symbol has to be quoted because otherwise it will be treated as a variable.
You can think of a function as a generalized version of a Lisp expression. The following expression tests whether the sum of 1 and 4 is greater than 3:
> (> (+ 1 4) 3) T
By replacing these particular numbers with variables, we can write a function that will test whether the sum of any two numbers is greater than a third:
> (defun sum-greater (x y z) (> (+ x y) z) SUM-GREATER > (sum-greater 1 4 3) T
Lisp makes no distinction between a program, a procedure, and a function. Functions do for everything. If you want to consider one of the functions as the main function, you can, but you will ordinarily be able to call any function from the toplevel. Among other things, this means that you will be able to test your programs piece by piece as you write them.