Upload
nguyendan
View
229
Download
4
Embed Size (px)
Citation preview
14/09/15
1
Introduction 2 Lisp Part I
Andreas Wichert
LEIC-T(Página da cadeira: Fenix)
Corpo docenten Andreas (Andrzej) Wichert –Praticas
n [email protected] tel: 214233231n room: N2 5-7n http://web.tecnico.ulisboa.pt/andreas.wichert/
14/09/15
2
Horário de dúvidas
n 5º-feira, 14H-17H, 2-N5.7
Guess:What does “LISP” stand for?
n Looney Idiotic Stupid Professor?
n Long Incomprehensible String of Parentheses?
n LISt Processing?
14/09/15
3
What is LISP? n Originated by John McCarthy (September
4, 1927 – October 24, 2011)n In 1959 as an implementation of recursive
function theory
n http://www-formal.stanford.edu/jmc/
14/09/15
4
What is LISP? n Conditionals - if-then-else constructn A function type - functions are first-class
objectsn Recursionn Typed values rather than typed variablesn Garbage collection (interpreter)n Programs made entirely of functional
expressions that return values
Uses Polish notation
n The description "Polish" refers to the nationality of logician Jan Łukasiewicz, (21 December 1878 – 13 February 1956) who invented (prefix) Polish notation in the 1920sn Polish notation, also known as Polish prefix
notation or simply prefix notation
14/09/15
5
n (5 − 6) × 7=
n Can be written as
n ( × (− 5 6) 7)
n When Polish notation is used as a syntax for mathematical expressions by programming language interpreters, it is readily parsed into abstract syntax trees and can, in fact, define a one-to-one representation for the same
14/09/15
6
n ((15 ÷ (7 − (1 + 1))) × 3) − (2 + (1 + 1)) = 5
n ((15 ÷ (7 − (1 + 1))) × 3) − (2 + (1 + 1)) = 5
n In Lisp
n (- (* (/ 15 (- 7 (+ 1 1))) 3) (+ 2 (+ 1 1))))5
14/09/15
7
Literaturen Practical Common Lisp, Peter Seibel,
2005, Apress, versa o online n ANSI Common Lisp, Paul Graham, 1995,
Prentice Hall, versa o online
n Introduca o a Linguagem Lisp , Anto nio Leita o, 1995, Instituto Superior Te cnico
My recommendation….LAND OF LISP, Conrad Barski, 2010, No Starch Press,http://landoflisp.com“A very good first contact with the world of Lisp!”"Turns out the border between genius and insanity is a pretty cheery place" says Paul Graham!
Land of Lisp - The Music Video (Home Work 1, watch the video)
14/09/15
9
n Dana Nau
n Introduction to Lisp / AI material
n https://www.cs.umd.edu/users/nau/
We will use Common lisp Jn Purpose of Common Lisp: to unify the
main dialects n Thus it contains multiple constructs to do
the same things n Common Lisp (CL) is a dialect of the Lisp
programming language, published in ANSI standard document ANSI INCITS 226-1994 (R2004) (formerly X3.226-1994 (R1999)).
14/09/15
10
Installing Common Lispn CLISP
n Download a CLISP installer from n http://clisp.cons.org/.
Running clisp
14/09/15
11
Clozure Common Lispn http://ccl.clozure.com
n Like all Common Lisp environments, CLISP will automatically place you into a read-eval-print loop (REPL)
n The code on the previous slide runs interpretively
• A few Common Lisps will compile the code every time you load it
n Compiling makes your programs run faster, and may detect some errors
14/09/15
13
Symbolsn Common Lisp symbols are typically made
up of letters, numbers, and characters like + - / * = < > ? ! _
n ice9, my-killer-app27, and even --<<==>>--.
14/09/15
14
Numbers> (+ 1 1.0) 2.0 > (expt 53 53) 243568481650227121324776065201047255185334531286856408445051308795767206091502 23301256150373
> (/ 4 6)2/3 > (/ 4.0 6) 0.6666667
> (- 5 6 7)-8
Why?Evaluates from left>(- (- 5 6) 7)-8
14/09/15
15
Strings(princ "Tutti Frutti")Tutti Frutti Tutti Frutti "Tutti Frutti"
n The text appears twicen First, we see the actual printing caused by
the princ command . n REPL will always show the result of
evaluating the entered expression• the princ function returns a value, the source
string.
> (princ "He yelled \"Stop that thief!\" from the busy street.")He yelled "Stop that thief!" from the busy street."He yelled \"Stop that thief!\" from the busy street."
14/09/15
16
Code Mode
n > (expt 2 3) n 8
n This calculates 2^3 = 8n It does this by calling expt, which
computes an exponent.
14/09/15
17
Data Mode > '(expt 2 3) (EXPT 2 3)The single quote tells Lisp to treat the subsequent form as a chunk of data—simply a list of items.
> (quote (expt 2 3))(EXPT 2 3)
> '(1 2 3)(1 2 3)
> ''(1 2 3)'(1 2 3)
14/09/15
18
But (Why? Read the error message)> ( 1 2 3)
*** - EVAL: 1 is not a function name; try using a symbol insteadThe following restarts are available:USE-VALUE :R1 Input a value to be used instead.ABORT :R2 Abort debug loopABORT :R3 Abort debug loopABORT :R4 Abort debug loopABORT :R5 Abort main loop
Predicates
14/09/15
19
Numerical predicates (> 5 4 3) T (> 5 4 6) NIL (<= 3 2) NIL (= 1 2) NIL (= 1 1 1) T >(zerop 0) T >(integerp 5)T
n > (eq 1234567890 1234567890)???
14/09/15
20
n The eq function is the simplest of all the Lisp comparison functions, and it’s also very fast.
n It doesn’t really work for comparing items besides symbols
n The eql command is similar to the eq command, but unlike eq, it also handles comparisons of numbers and characters
n The equalp command is essentially the same as the equal command, except that it can handle some difficult comparison cases with a bit of extra sophisti- cation
14/09/15
21
Atoms n Every Lisp object is either an atom or a
list
Lists in Lisp n A cons cell looks like this:
n We create the list '(1 2 3)
14/09/15
23
The cons Function link any two pieces of data
>(cons 'chicken 'cat) (CHICKEN . CAT) > (cons 'chicken 'nil) (CHICKEN) > (cons 'chicken ()) (CHICKEN) > (cons 'beef (cons 'chicken nil))(BEEF CHICKEN)
> (cons 'beef (cons 'chicken nil))(BEEF CHICKEN)
>(cons 'pork '(beef))(PORK BEEF)
> (cons 'pork '(beef chicken))(PORK BEEF CHICKEN)
14/09/15
24
The car and cdr Functions > (car '(pork beef chicken)) PORK
> (cdr '(pork beef chicken)) (BEEF CHICKEN)
> (cdr '(pork beef chicken)) (BEEF CHICKEN) > (car '(beef chicken)) BEEF > (car (cdr '(pork beef chicken))) BEEF > (cadr '(pork beef chicken)) BEEF
14/09/15
25
Nested Lists n Lists can contain other lists.
'(cat (duck bat) ant)
>(car '((peas carrots tomatoes) (pork beef chicken))) (PEAS CARROTS TOMATOES)
14/09/15
26
if> (if (= (+ 1 2) 3) 'yup 'nope) YUP
>(if (= (+ 1 2) 4) 'yup 'nope) NOPE
>(if '(1) 'the-list-has-stuff-in-it 'the-list-is-empty) THE-LIST-HAS-STUFF-IN-IT
>(if '() 'the-list-has-stuff-in-it 'the-list-is-empty) THE-LIST-IS-EMPTY
14/09/15
27
> (if (oddp 5) 'odd-number 'even-number) ODD-NUMBER
> (if (oddp 5) 'odd-number (/ 1 0)) ODD-NUMBER
Why? Special form, executes only the true branch
14/09/15
30
To load / compilen (load <nome-ficheiro>)
n (load “projecto.lisp”)
n (compile-file "projecto")
n (load "projecto")