You are on page 1of 13

(Brief) Introduction to LISP

CS 441/541
Artificial Intelligence
Winter 2015
!
Slides by Kendall Stewart
References
• Most of the material in these slides comes from the
book “AI Algorithms, Data Structures, and Idioms in
Prolog, Lisp, and Java” by George F. Luger and
William A. Stubblefield (not our course textbook,
but same author)

• The text of this book is freely available on Prof.


Luger’s website: www.cs.unm.edu/~luger/

• There’s plenty of good historical and background


information on Wikipedia as well.
History
• LISP (the LISt Processing Language) was originally
invented in 1958 at MIT by John McCarthy, a mathematician
and one of the founders of the field of Artificial Intelligence

• Designed to be an implementation of Alonzo Church’s


Lambda Calculus (a theoretical model of computation
equivalent in power to a Turing Machine)

• Today: LISP (usually, and hereafter, just “Lisp”) is a family of


general-purpose functional programming languages, the
most popular dialects of which are Common Lisp, Scheme,
and Clojure

• Luger’s “Idioms” book uses Common Lisp.


Features
• Lisp is a functional language: “its syntax and semantics are
derived from the mathematical theory of recursive
functions” (Luger p.149). In general, this means no iterative
looping: the main tool for repetition is recursion.

• Unlike other functional languages such as Haskell, Lisp


functions are not mathematically “pure”. That is, Lisp
functions can have side effects, or return different values
when given the same arguments.

• Lisp is dynamically typed. That is, the type of an expression is


not known until runtime. This is flexible, but also dangerous: it
is possible to define a function that is impossible to evaluate,
like trying to divide a number by a string.
Installing Common Lisp
• Because of Lisp’s long history (and relative simplicity), there
are dozens of Common Lisp implementations available.

• Nevertheless, it’s nice to have one with good features: for


instance, I like CLISP (www.clisp.org) for its simplicity, and

because it shows you when you match parentheses.


CLISP is available through your OS’s package manager (if
you are using Linux) and through Homebrew (on OSX). It is
also available as a Windows installer.

• If you cannot run an installer (e.g. if you are on a lab machine


and do not have administrator access), lispbox (common-
lisp.net/project/lispbox) is a Common Lisp IDE that does not
require installation.
Syntax
• Lisp has an incredibly simple syntax, the basic units of which are “symbolic
expressions” (s-expressions)
• An s-expression is defined recursively, as either:
๏ an atom (a number, string, or “symbol”)

๏ a parenthesized list of s-expressions, separated by spaces

• Examples of s-expressions (adapted from Luger p.151):


๏ 3.1416

๏ 100

๏ “hello”

๏ (george kate “james joyce”)

๏ (1 2 3 4)

๏ hyphenated-name

๏ *some-global*

๏ (a (b c) (d (e f)))

๏ nil

๏ ()

๏ (* 3 (/ 1 2))
Evaluation
• s-expressions are evaluated as follows:

๏ Atoms evaluate to their value. If the atom is a


symbol, it must be bound to a value.

๏ Each element of a list (s1 s2 … sn) is evaluated


to values v1,v2,…,vn. v1 must be a function,
which is then applied to v2 through vn.

• The order of evaluation does not matter so long as


it happens before function application.
Example: (3 + 5)
1. Evaluate 3
1. 3 evaluates to the number three.
2. Evaluate +
1. + is a symbol. 

Lookup + in the symbol and find the addition function.
3. Evaluate 5
1. 5 evaluates to the number five.
4. Error! The number three is not a function.
Arithmetic in Lisp must be done with fully parenthesized “Polish
Prefix” notation. The disadvantage is that it might be unfamiliar. The
advantage is that it is utterly unambiguous — there is no need for
operator precedence rules.
Example: (- (+ 3 5) 7)
1. Evaluate -
1. - is a symbol. 

Lookup - in the symbol table and find the difference function.
2. Evaluate (+ 3 5)
1. Evaluate +
1. + is a symbol. 

Lookup + in the symbol and find the addition function.
2. Evaluate 3
1. 3 evaluates to the number three.
3. Evaluate 5
1. 5 evaluates to the number five.
4. Apply the addition function to the numbers three and five.
5. The result is the number eight.
3. Evaluate 7
1. 7 evaluates to the number seven.
4. Apply the difference function to the numbers eight and seven.
5. The result is the number one.
Example: (nth 1 ‘(alice bob mallory))

1. Evaluate nth: lookup nth, find the list indexing function

2. Evaluate 1: the number one

3. Evaluate ‘(alice bob mallory)

1. ‘ means quote, i.e. do not evaluate.

2. Therefore ‘(alice bob mallory) evaluates to the list 



(alice bob mallory), which is a list of atoms (symbols).!

4. Apply the list indexing function to the number one and the list
(alice bob mallory)

5. The result is the atom bob, a symbol.


Defining Functions
• Functions can be defined with a defun expression:

(defun <function name>

(<formal parameters>) <function body>)

• Example (from Luger):



(defun square (x) (* x x))

• This defines the function symbol square in the


environment, where it can now be used in an s-
expression:

(square 5) ; will return 25 (this is a comment)
Special Forms
• To control evaluation, Lisp has special forms like 

if and cond (other special forms exist as well):

(defun yup-nope (x) 

(if (< x 0) “yup” “nope”)

)

(list (yup-nope 3) (yup-nope -8)) ; returns (“nope” “yup”)

• (defun which-mult (x)



(cond ( (= (mod x 2) 0) "multiple of 2" )

( (= (mod x 3) 0) "multiple of 3" )

( t "something else" ) ; t means “true”

)

)

(which-mult 6) ; returns “multiple of 2”

(which-mult 9) ; returns “multiple of 3”

(which-mult 7) ; returns “something else”
Further Reading
• This is just a brief introduction to the syntax of Common Lisp!

• There are many more features that make it suitable for AI


programming, such as the ability to write higher order functions or
extend the language with meta-interpreters for specific tasks.

• For this course: “AI Algorithms, Data Structures, and Idioms in


Prolog, Lisp, and Java” by George F. Luger and William A.
Stubblefield (http://www.cs.unm.edu/~luger/)

• For Lisp as a general-purpose programming language: “Practical


Common Lisp” by Peter Seibel (http://www.gigamonkeys.com/book/

• Scheme programmers might find the following page useful as an


introduction to the differences between Scheme and Common Lisp:

http://www.cs.utexas.edu/~novak/schemevscl.html

You might also like