LISP
worksheet #1, worksheet #2, worksheet #3, worksheet #4
LISP stands for LISt Processing language, which was developed in the 1950's. It is still used in artificial intelligence (AI) programming today. LISP follows a functional paradigm of programming rather than procedural (like older versions of BASIC and object-oriented like C++.)
Everything in a LISP program is either an atom or a list. Lists are actually groups of atoms. A LISP program is simply a set of functions which must be evaluated from innermost parentheses outwards in a left to right manner.
A few important functions that are defined in LISP are:
Function | Description | Example | Visual Basic equivalent |
SETQ | serves as the assignment operator; it "sets" a variables value | (SETQ a 22)
a is assigned the value of 22 |
a = 22 |
ADD | adds two atoms | (ADD a b)
a and b are added together |
a + b |
SUB | subtracts two atoms | (SUB m 3)
3 is subtracted from m |
m - 3 |
MULT | multiplies two atoms | (MULT 2 14)
2 is multiplied with 14 to obtain 28 |
2 * 14 |
DIV | divides two atoms | (DIV 24 2)
24 is divided by 2 to obtain 12 |
24 / 2 |
SQUARE | squares a number | (SQUARE 3)
3 is multiplied with itself to obtain 9 |
3 ^ 2 |
CAR | strips the first atom off of a list | (CAR (2 3 4))
2 is the result since it is the first atom of the list (2 3 4) |
n/a |
CDR | strips the first atom off of a list but returns the remaining list | (CDR (2 3 4))
the list (3 4) is the answer since the first atom 2 is stripped off and "thrown away" |
n/a |
EXP | computes the power of two arguments |
(EXP 2 3) 8 is the result since 2 to the third power is 8 |
2 ^ 3 |
REVERSE | reverses each of the atoms within a list | (REVERSE (2 3 4))
results in the list (4 3 2) |
n/a |
' (apostrophe) | the apostrophe acts similarly to double quotes in BASIC or C++; it tells you to treat what follows it as one list and not to evaluate that list | (SETQ A (MULT 2 3))
causes A to be assigned the value 6 (SETQ A '(MULT 2 3)) causes A to be assigned the list (MULT 2 3) which is not evaluated due to the single apostrophe |
n/a |
DEF | defines a function | (DEF addsOne(x) (ADD x 1)) | Function addsone(x As Integer) As Integer addsOne = addsOne + 1 End Function |
Example LISP statements and the resulting returned values:
LISP statement | Final returned value | Explanation |
(SETQ a (MULT 2 3) | 6 | a is an atom with a value of 6 |
(SETQ a '(MULT 2 3) | (MULT 2 3) | a is a list with 3 atoms |
(SETQ b 'a) | a | b is now an atom with a value of the character a (not the variable a) since a single apostrophe preceded a |
(SETQ c a) | (MULT 2 3) | c is a list with 3 atoms |
(SETQ TEST (ADD 3 (MULT 2 5))) | 13 | test has a value of 13 |
(SETQ VOWELS '(A E I O U)) | (A E I O U) | VOWELS is a list of 5 atoms |
(SETQ x (SETQ y 'same)) | same | both x and y contain the string literal same |
(SETQ y 'diff) | diff | y is now the string literal diff |
(SETQ x y) | diff | x is now the string literal diff also |
(SETQ x (ADD 45 8)) | 53 | x is now 53 |
(SETQ Y '(ADD X 11)) | (ADD X 11) | Y is a list with 3 atoms (uppercase Y that is) |
(REVERSE '(8 7 6)) | (6 7 8) | |
(REVERSE '(1 (2 3 4) 5 6) | (6 5 (2 3 4) 1) | do not reverse the atoms 2, 3, & 4 which are contained in the list (2 3 4) since the list (2 3 4) is actually an atom of the whole list to be reversed |
(CAR '(This is a list)) | This | The final value here is an atom This not a list since the function CAR always returns an atom |
(CAR (CDR '(This is a list))) | is | |
(SETQ x (CDR (CDR '(a b c d)))) | (c d) | x is now the list (c d) |
(CDR '(This is a list)) | (is a list) | CDR always returns a list |
(CAR '(hi)) | hi | |
(CDR '(one)) | NIL | if you take out the first element of the list (one) you have an empty list ( ) which is referred to as NIL by LISP |
(CAR '((1) (2 3) (4 5 6))) | (1) | Even though CAR always returns an atom, the first atom of the list ((1) (2 3) (4 5 6)) is the list (1) |
(CDR (CAR '(( red white) blue))) | (white) | |
(CAR 'hi) | error | CAR can only be applied to lists and hi is not a list |
(CAR ( ) ) | error | CAR cannot return an atom as the final answer here since there are no atoms in an empty list |
(CAR '(ADD 2 2)) | ADD | |
(CDR '(( red green) (blue white)) | ((blue white)) | |
(SETQ area (MULT 2 3)) | 6 | |
(SETQ fruit '(apple orange)) | (apple orange) |
Associated LISP references:
Gary Steele's LISP
Programming Textbook
An interactive
LISP tutorial - see this
page for the interactive part
ACSL Home Page | Mr. Minich's Wyo Home Page | Minich.com Web Design