Tải bản đầy đủ (.pdf) (66 trang)

Introduction to lisp ebook

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (324.81 KB, 66 trang )

Last update: February 16, 2010
Introduction to Lisp
Dana Nau
Dana Nau 1
Outline
♦ I assume you know enough about computer languages that you
can learn new ones quickly, so I’ll go pretty fast
♦ If I go too fast, please say so and I’ll slow down
Assignment:
1. Get a TerpConnect account if you don’t already have one
2. Start reading one or more of the following (you’ll need to figure out
which parts correspond to my lecture)
• ANSI Common Lisp - available at the bookstore
• Common Lisp the Language, 2nd edition (URL on the class page)
• Allegro Documentation (URL on the class page)
3. Read Norvig’s tutorial on Lisp programming style (URL on the class page)
Dana Nau 2
What does “LISP” stand for??
Dana Nau 3
What does “LISP” stand for??
A speech defect in which you can’t pronounce the letter ‘s’?
Dana Nau 4
What does “LISP” stand for??
A speech defect in which you can’t pronounce the letter ‘s’?
Looney Idiotic Stupid Professor?
Dana Nau 5
What does “LISP” stand for??
A speech defect in which you can’t pronounce the letter ‘s’?
Looney Idiotic Stupid Professor?
Long Incomprehensible String of Parentheses?
Dana Nau 6


What does “LISP” stand for??
A speech defect in which you can’t pronounce the letter ‘s’?
Looney Idiotic Stupid Professor?
Long Incomprehensible String of Parentheses?
LISt Processing?
Dana Nau 7
What is LISP?
Originated by John McCarthy in 1959 as an implementation of recursive
function theory.
The first language to have:
• Conditionals - if-then-else constructs
• A function type - functions are first-class objects
• Recursion
• Typed values rather than typed variables
• Garbage collection
• Programs made entirely of functional expressions that return values
• A symbol type
• Built-in extensibility
• The whole language always available – programs can construct
and execute other programs on the fly
Most of these features have gradually been added to other languages
Dana Nau 8
LISP’s influence on other languages
It seems to me that there have been two really clean, consistent mod-
els of programming so far: the C model and the Lisp model. These
two seem points of high ground, with swampy lowlands between them.
As computers have grown more powerful, the new languages being de-
veloped have been moving steadily toward the Lisp model. A popular
recipe for new programming languages in the past 20 years has been to
take the C model of computing and add to it, piecemeal, parts taken

from the Lisp model, like runtime typing and garbage collection.
– Paul Graham, The Roots of Lisp, May 2001
We were after the C++ programmers. We managed to drag a lot of
them about halfway to Lisp.
– Guy Steele, co-author of the Java spec
More quotes at />Dana Nau 9
LISP applications
AI programs often need to combine symbolic and numeric reasoning.
Lisp is the best language I know for this.
♦ Writing SHOP (my group’s AI planning system) took a few weeks in Lisp
♦ Writing JSHOP (Java version of SHOP) took several months
Lisp is less used outside of AI, but there are several well-known LISP appli-
cations:
♦ AutoCAD - computer-aided design system
♦ Emacs Lisp - Emacs’s extension language
♦ ITA Software’s airline fare shopping engine - used by Orbitz
♦ Parasolid - geometric modeling system
♦ Remote Agent software - deployed on NASA’s Deep Space 1 (1998)
♦ Script-Fu plugins for GIMP (GNU Image Manipulation Program)
♦ Yahoo! Merchant Solutions - e-commerce software
Dana Nau 10
Why learn LISP?
Several universities teach Scheme (a dialect of Lisp) in their introductory
Computer Science classes
LISP is worth learning for a different reason — the profound enlighten-
ment experience you will have when you finally get it. That experience
will make you a better programmer for the rest of your days, even if
you never actually use LISP itself a lot.
– Eric Raymond, How to Become a Hacker, 2001
Dana Nau 11

More about Lisp and Enlightenment . . .
From />Dana Nau 12
Common Lisp
♦ Lisp’s uniform syntax makes it very easily extensible
Just write new functions and include them when launching Lisp
♦ This led many groups to create their own Lisp dialects:
BBN-Lisp, Franz Lisp, Interlisp-10, Interlisp-D, Le-Lisp, Lisp 1.5,
Lisp/370, Lisp Machine Lisp, Maclisp, NIL, Scheme, T, ZetaLisp, . . .
⇒ problems with incompatibility
♦ Purpose of Common Lisp: to unify the main dialects
Thus it contains multiple constructs to do the same things
You’ll be using Allegro Common Lisp on solaris.grace.umd.edu
Documentation: links on the class page
Dana Nau 13
Launching Allegro Common Lisp
Login to solaris.grace.umd.edu using your TerpConnect account
You’ll be using Allegro Common Lisp. Here is how to launch it:
tap allegro81
alisp
To avoid having to type tap allegro81 every time you login, put it into the
.cshrc.mine file in your home directory
Running Common Lisp elsewhere:
♦ Allegro Common Lisp is installed on some of the CS Dept computers
e.g., the junkfood machines
♦ You can also get a Common Lisp implementation for your own computer
Check “implementations” on the class page
But make sure your program runs correctly using alisp on
solaris.grace.umd.edu, because that’s where we’ll test it.
Dana Nau 14
Starting Out

♦ When you run Lisp, you’ll be in Lisp’s command-line interpreter
♦ You type expressions, it evaluates them and prints the values
sporty:
~
: alisp
. . . several lines of printout . . .
CL-USER(1): (+ 2 3 5)
10
CL-USER(2): 5
5
CL-USER(3): (print (+ 2 3 5))
10
10
CL-USER(4): (exit)
; Exiting Lisp
sporty:
~
:
Some Common Lisps also have GUIs; check the documentation
Dana Nau 15
Atoms
♦ Every Lisp object is either an atom or a list
♦ Examples of atoms:
numbers: 235.4 2e10 #x16 2/3
variables: foo 2nd-place *foo*
constants: pi t nil :keyword
strings, chars: "Hello!" #\a
arrays: #(1 "foo" A) #1A(1 "foo" A) #2A((A B C) (1 2 3))
structures: #s(person first-name dana last-name nau)
♦ For Lisp atoms other than characters and strings, case is irrelevant:

foo = FOO = Foo = FoO = . . .
pi = Pi = PI = pI
2e10 = 2E10
Dana Nau 16
Lists
(a
1
a
2
a
3
. . . a
k
) =⇒

a1 a2 a3 ak
NIL
a
1
, a
2
, . . . , a
k
may be atoms or other lists
A list of one element: (a) =⇒
a
NIL
The empty list is called () or NIL; it’s both a list and an atom
Examples:
(235.4 (2e10 2/3) "Hello, there!" #(1 4.5 -7))

(foo (bar ((baz)) asdf) :keyword)
Dana Nau 17
Dot notation
If the last pointer points to something other than nil, it’s printed with a dot
before it
(a b c d . e) =⇒
a b c d
e
(a b c d . NIL) = (a b c d)
(a . b) =⇒
a
b
Example:
(235.4 (2e10 2/3) "Hello, there!" #(1 4.5 -7) . foobar)
Dana Nau 18
Defining Lisp Functions
(defun fib (n)
(if (< n 3)
1
(+ (fib (- n 1))
(fib (- n 2)))))
This is a very bad code; its running time is exponential in n. My only purpose
is to give an example that you can understand without knowing Lisp.
Suppose the definition is in a file called fibonacci.cl
sporty:
~
: alisp
several lines of printout
CL-USER(1): (load "fibonacci")
; Loading /homes/research/nau/fibonacci.cl

T
CL-USER(2): (list (fib 1) (fiB 2) (fIb 3) (fIB 4) (Fib 5) (FiB 6))
(1 1 2 3 5 8)
CL-USER(3):
Dana Nau 19
Compiling
♦ The code on the previous slide runs interpretively

Compiling makes your programs run faster, and may detect some errors
♦ To compile fib after it has been loaded, we can use (compile 'fib)
Later I’ll explain what the ' is for
♦ That only compiles the code that’s running in the current Lisp session.
If you start up a new Lisp session and load fibonacci.cl again,
it will run interpretively again.
♦ To compile the entire fibonacci.cl file, use (compile-file "fibonacci")
This creates a binary file called fibonacci.fasl
∗∗
————–

A few Common Lisps will compile the code every time you load it. Allegro doesn’t.
∗∗
Other Common Lisps may use different file-naming conventions.
Dana Nau 20
Loading
♦ (compile-file "fibonacci") doesn’t load the file
You need to do that separately
♦ The next time you do (load "fibonacci"), it will load fibonacci.fasl
instead of fibonacci.cl
♦ In Allegro Common Lisp, (load "fibonacci") does the following:


load fibonacci.fasl if it exists
else load fibonacci.cl if it exists
else load fibonacci.lisp if it exists
else error
♦ Use (load "fibonacci.cl") to specify the exact file,
(load "foo/fibonacci") to specify a file in a subdirectory,
etc.
————–

Details (e.g., file suffixes) may vary in other Common Lisps.
Dana Nau 21
Style
♦ Read Norvig’s tutorial on Lisp programming style
There’s a link on the class page.
♦ Examples of comments, variables, and indenting:
;;; A comment formatted as a block of text
;;; outside of any function definition
(defun fib (n)
;; A comment on a line by itself
(if (< n 3)
1 ; A comment on the same line as some code
(+ (fib (- n 1))
(fib (- n 2)))))
(setq *global-variable* 10)
(let (local-variable)
(setq local-variable 15))
Dana Nau 22
Editing Lisp files
Use a text editor that does parenthesis matching
Emacs is good if you know how to use it, because it knows Lisp syntax

Parenthesis matching
Automatic indentation
Automatic color coding of different parts of the program
But if you don’t already know emacs,
Don’t bother learning it just for this class
Steep learning curve
Emacs’s built-in Lisp is not Common Lisp. Don’t use it for your projects!
Dana Nau 23
Development Environments
If you use Eclipse, there are two Lisp plugins for it:
♦ Cusp
♦ Dandelion
I don’t use Eclipse, so I don’t know much about them
If you use Emacs, there are two macro packages you can use:
♦ The one that comes with Allegro Common Lisp
♦ SLIME
These can run Common Lisp in an Emacs buffer, and do various other things
The class home page has links to all of these
Dana Nau 24
Lisp functions
Next, I’ll summarize some basic Common Lisp functions
♦ I may leave out some details
♦ There are many more functions than the ones I’ll discuss
♦ For more information, see the list of sources at the start of this lecture
Dana Nau 25

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×