Simple expressions


Here are some examples (repeated in part from quoted expressions): user> nil () user> t #t user> ticks-per-second 1000.00000000000

Function applications

It is often said that Lisp has a simple syntax. That is a subjective assessment with which many people would disagree, but it certainly has a regular and predictable syntax. This stems from the fact that all operations are written as prefix forms, ie. operator followed by operands. However rather than the function appearing in front of a list of arguments enclosed in parentheses: function(argument-1, ... , argument-n) it moves inside the first parenthesis: (function argument-1 ... argument-n) Also note that arguments are separated by spaces, not commas. As in most other languages, the arguments can be arbitrary expressions. In some Lisps, the function may also be an arbitrary expression just like the arguments, in others special notation may be required. This last point will be addressed again in the definitions page.

Here are some examples:

user> (+ 1 2) 3 user> (+ 2 (* 3 4.5)) 15.5000000000000 user> ((if t + -) 2 3) 5 user> ((if () + -) 2 3) -1

The apply function

The apply function takes a variable number of arguments, the first one of which must be a function, and the last one must be a list. Its action is to apply the function to these arguments, the important bit being that the elements of the final argument to apply constitute individual arguments, not a list. That sounds a little confusing, so here are some examples: user> (apply + 1 2 (list 3 4)) 10 user> (apply list 1 2 (list 3 4)) (1 2 3 4) user> (list 1 2 (list 3 4)) (1 2 (3 4)) The first illustrates how the arguments from the list are spread to be presented to the function +. The point of the second and third input lines is to contrast the use of apply with straight function application.
Julian Padget,, this version January 13, 1995