Lisp Programming/Beginning Lisp
Lisp syntax is very simple and easy to understand. A list is a series of atoms surrounded by parentheses, and the first atom of a list is a function. Any atoms after the first are parameters of the function. An example Lisp expression to add the numbers 1 and 2 together might look like this:
(+ 1 2)
As you can see, the expression is a list. The first atom is +, which is also a function to be evaluated. 1 and 2 are the parameters of the + function.
You can also use a list as a parameter of a function like this, which adds the numbers 1 and 2 together, but somewhat more fancily:
(+ 1 (+ 1 1))
In this example, the second parameter of the + function is a list. The first atom is a function: +. So the inner list is evaluated to two (1+1=2), and then that result is used as the second parameter of the outer list, which then gives the result 3.
There are of course many other functions in Lisp. Some of the most basic are these: +, -, /, *, quote, set, and cons. It should be obvious what the +, -, *, and / functions do and how they work.
The quote function returns its first argument, unevaluated. This is useful for passing a form (expression) to a function, without first evaluating that form (expression).
(quote (param1 param2))
quote can also be abbreviated as '. ' goes "outside" the list that it acts on. So
just like in the first example.
The cons function constructs a list by adding an item to a list. Adding an item to the empty list '() creates a new list.
(cons 1 '())
(cons 1 (cons 2 (cons 3 '())))
(1 2 3)
Many of the attributes of Lisp are due to the fact that lists are constructed of conses.
Variables[edit | edit source]
In Lisp, there are several ways to set a variable. Here are the most common.
Setq sets a variable to a given value. If the variable doesn't exist, then it is created. All variables created with setq are global. Setq works like this:
(setq variable-name <value>)
An example bit o' code using setq would be this:
(setq camel "I am a camel!") (print camel)
Which should print "I am a camel!" when it executes.