This tutorial is an introduction to Lisp features. if you have a previous background programming with Java or C/C++, it will help you to get up and coding on LISP quickly .
1. Install a LISP interpreter
In this tutorial, I am using SBCL (Steel Bank Common Lisp) – Windows version 1.3.18. It can downloaded here: http://www.sbcl.org/platform-table.html
Once you install it, click the start button and execute it. It will open a console running the LISP interpreter as shown in Figure 1. Lisp has a command line where you type in things to execute.
2. Atomic Expressions
An expression is a finite well-formed combination of symbols that is able to be evaluated (executed). All expressions return a value. When an expression is typed at the command line, LISP evaluate it and print its value.
2.1 Numbers are expressions.
The value of a number is the number itself. LISP can represent many kind of numbers including integers, floating point, fractions, big integers, and complex numbers.
- A fraction is represented with the form x/y – the “/” does not mean “division”.
- A complex number a+bi is represented as #C(a b)
- A floating point number can be represented using decimal point and/or the e notation.
2.2. Characters are expressions.
Like a number, the value of a character is itself. A character is represented using the #\ syntax. For instance, #\A is the character ‘A’. #\! is the character ‘!’. There are control characters, they have a name, such as:
2.3. Strings are expressions.
Just like a numbers and characters, the value of a string is itself. A Lisp string is a sequence of characters. Lisp strings begin and end with double-quotes. Like C++ and Java, Lisp strings have escape sequences to put special characters in the string. Escape sequence begins with a backslash \. For instance, to put a double-quote in the middle of a string, the sequence is \”. To put a backslash in the middle of a string, the sequence is \\. Unlike C++, returns and tabs are not added to strings using an escape sequence. Instead, the tab or the return are typed right in the string itself.
2.4. Boolean values.
In Lisp, the special constant nil (case insensitive) represents “false”. nil evaluates to itself. Every other expression but nil is considered to be “true”. However, Lisp also provides a constant which represents “true”. This is t (also case-insensitive). t also evaluates to itself.
3. Lists as Functions
Lisp program code takes the form of lists. A list begins with a parenthesis, then immediately contains a function name, then zero or more expressions (parameters) separated with whitespace, then a closing parenthesis. When Lisp evaluates a list, it first examines the symbol at the beginning of the list. Usually this symbol is associated with a function name or operator. Then each expression in the list (except the beginning symbol) is evaluated. The values of these expressions are then passed in as parameters to the function, and the function is called. The list’s return value is then the value returned by the function. Like everything else in Lisp, lists are expressions. This means that lists return a value when evaluated. Figure 2 shows two examples of functions in LISP.
* (+ 1 2 3 4 5) 15 * (* 2 2) 4 * (sqrt 2) 1.4142135 * (SQRT 2) 1.4142135 * (Sqrt 2) 1.4142135 *
In C++ and in Java, there are operators like +, -, &&, etc. But in Lisp, there are no operators; instead, there are only functions. For instance, + and * are functions, and denote the addition and multiplication functions respectively. Thus, It can be wrote: (+ 1 2 3 4 5) or (* 2 2).
Some functions have a fixed number of arguments and other ones (like + or *) can have any number of arguments.
Function names are not case-sensitive. Thus, it is possible to write sort or SQRT or Sqrt and obtain the same result.
Lisp has a special name for functions which return “true” (usually t) or “false” (nil). These functions are called predicates. For instance, the operator = or <. But, also there are Lisp defined functions that are predicates such as oddp that evaluates if an expression value is odd or numberp that evaluates if an expression value is a numeric value.
* (= 20 30) NIL * (< 3 4) T * (numberp "hello") NIL * (oddp 333333) T
When a list contains another list among its expressions, the evaluation procedure is recursive. For instance:
* ( + (* 2 2) (* 3 3) ) 13
Expressions may use multiple lines; for instance, the example below add the length of the string “hello” (5) and the length of the string “yes” (3) and the value 10.
* (+ (length "hello") (length "yes") 10 ) 18
A particularly useful function is print. It evaluates its argument, then prints it, then returns the argument.
If it is just used by itself, the screen will appear to print the element twice. It’s because print printed its argument, then returned it, and Lisp always prints the final return value of any expression.
Print can be stick it in the middle of an expression, where it will print elements without effecting the final return value of the whole expression. See the examples below:
4. Control Structures
7. Lists as Data
8. Loading and Compiling Lisp
9 Arrays and Vectors
10. Setf and Friends
11. Function, Funcall, and Apply
13. Lambda and Closures
14. Sequence Functions
15. Functions With Variable Arguments
16. Predicates and Types
17. Hash Tables
18. Printing and Reading
19. More Control Structures
20. Writing Lisp in Lisp