What is LISP?
• A LISt Processing language
– The basic data structure is linked list and Atoms.
• A functional programming language
– Each expression in LISP is a function that returns a value
• An interpretive language
– Running LISP programs involves interacting with the LISP
– Clisp is the common lisp interpreter available only for Linux.
– Recently Compilers have been made for this language but
they are not used a lot.
• First developed by John McCarthy as a language for symbolic
(rather than numeric) computation in 1958 based on Fortran
• Very SLOW:
– no compilation of source to machine code
– inefficient garbage collector
• Historical Reason
– Most AI programs in the U.S. have been developed in LISP
– However, most AI programs in Europe have been developed
•Write a program => Define a set of
•Running a program => Evaluating an
•Simple syntax: operator precedence issues
•Lists (or S-expressions) are important:
•Functions are defined as lists
•Lists can be manipulated easily in Lisp
•Functions can be manipulated easily
• Lisp is a functional language – So everything is a function
-> Functions in other languages
var sum := sqrt(x)
In lisp this function is:
-> (sqrt x)
• Most LISP functions require you to think and use Prefix
Notation – you have to think backwards
– (print (+ 5 (sqrt 6)))
• And there are primary effects and side effects
– In ‘pure’ Lisp, we ONLY get values by executing a function.
– We DON’T set a variable to a new value INSIDE a function….that is a
side effect of executing a function. (You all probably do that all the
time in Java or C++)
How the Lisp Functions work –
The read-eval loop
• Evaluation process starts with an “S”
expression (i.e., a function and operands to
that function) This one adds two numbers….
(+ 3 4)
Parentheses – Notification to evaluate
Function name – Go get function (in this case ‘+’ is
the add function)
space - separator
operands -- value for function
Parentheses – stop evaluation and return the
How Lisp Interpreter Works
•Programs are lisp objects (i.e.
•Evaluation of a lisp object returns a
•Evaluation is simply a function called
EVAL that maps lisp objects to lisp
•EVAL: object => object
•we will use the symbol => to
•The Lisp interpreter is a loop
•read a lisp expression
Let’s start with the math
• There are many built in arithmetic functions
• You then put these arithmetic functions
(+ numbers…) -- adding
(- numbers…) -- subtracting
(* numbers…) -- multiplication
(/ numbers…) -- division
(1+ number) – plus 1 (this is hard to read)
(1- number) – minus 1
(abs number) etc…..
» examples: 3, 8.9, etc.
» An object written as a sequence of characters
» Symbols are usually manipulated as names that
are “bound” to other lisp objects
• Symbol FOO might be bound to 4.2
•Anything with parentheses
•(this is one too)
•(a list of (two) lists)
•(a (very (very (very
A Special Symbol: NIL
• NIL represents an empty list.
• NIL is a terminator of a list.
• A list is usually built by inserting its elements
into NIL in the reverse order .
• NIL can also represent “false''.
• The special symbol representing “true” is T.
Taking lists apart
• (first <a list>) returns the first element of the
• (rest <a list>) returns the remaining list (i.e.,
everything except the first element).
• Quote symbol ‘ is a short hand for the
function called QUOTE.
• (QUOTE <arg>)
• QUOTE is a special function that prevents
LISP from evaluating its argument.
• QUOTE returns the argument literately.
Example: (quote (dummy-fn 2))
==> (DUMMY-FN 2)
Basic Evaluation Rules
• A number evaluates to itself
• A symbol evaluates to its value.
• A list is evaluated by
– treating the first element as a function
– evaluating each arguments of the function in a left-to-right
• An expression preceded by a quote symbol ‘
evaluates to the expression itself.
Assignment and Binding
• A symbol (or variable) can be assigned a
value (called its binding) using SETQ.
• (SETQ <symbol-name> <anything>)
Example: (SETQ A ‘(A B C)) ==> (A B C)
A evaluates to ==> (A B C)
• Evaluating a symbol that does not have a
value assigned (i.e., no binding) causes error
All other functions do NOT
change the bindings
• In particular, FIRST and REST are non-
> (setq my-friends ‘(Superman Batman Robin) )
(Superman Batman Robin)
> (first (rest my-friends))
(Superman Batman Robin)
Defining My-Own Functions
• A function is defined using DEFUN
• (DEFUN <fn-name> (<arg1> ...<argK>)
<exp1> ... <expN> )
• All arguments are passed by value.
• The body of the function may contain any
number of expressions (i.e., function calls).
• The function returns the value returned by the
Defining A Function
(defun square (x)
(times x x) )
(defun add-friend (new-friend friends)
(cons new-friend friends) )
Predicates – Checking to see if
something is true….
• Functions that return ``true’’ (i.e., T) or
``false’’ (i.e., NIL).
• (NULL <item>) returns T if <item> is NIL
(empty list), otherwise NIL.
• (LISTP <item>) returns T if <item> is a list,
• (ATOM <item>) returns T if <item> is an atom
(i.e., a symbol, a number, or NIL).
• (NUMBERP <item>) returns T if <item> is a
• COND is an N-branch conditional expression
(COND ( <test1> <exp11> ... <exp1L> )
( <test2> <exp21> ... <exp2M> )
( <testK> <expK1> ... <expKN> ) )
• Each test is evaluated sequentially until a test
• Expressions following that test will be
• COND returns the value returned by the last
expression associated with the test.
Terminates a COND with a T
(defun select-character (enemy)
(cond ( (equal enemy ‘Penguin)
( (equal enemy ‘Catwoman)
( (equal enemy ‘Black-Knight)
‘(White-Knight King-Arthur ) )
( T ; for all other enemies
‘SuperMan) ; ask Superman for help
Using AND, OR in COND
(defun Evaluate-feeling ( sentence )
(cond ( (OR (member ‘hate sentence)
(member ‘dislike sentence))
( (AND (member ‘I sentence)
(member ‘feel sentence) )
) ; end of cond
) ; end of defun
i=1, while i <= 3:
=> (loop for i in ‘(1 2 3) do (print i))
1 2 3
i=1, while i <= 3: (different step)
=> (loop for i from 1.0 to 3.0 by 0.5 do
i=3, while i >= 1:
=> (loop for i from 3 downto 1 do (print i))
Things to remember:
•Lisp is considered the mother of a lot of
functional languages like Scheme and Haskell …..
•Common Lisp has dynamic type checking
•Lisp is interpreted.
The reason why Lisp is dead:
•Stock hardware platforms becoming faster than
special purpose hardware
•Low Interoperability with other languages