Introduction 2 Lisp Part I Andreas Wichert LEIC-T (Página da cadeira: Fenix) Corpo docente n Andreas (Andrzej) Wichert Praticas n andreas.wichert@tecnico.ulisboa.pt n tel: 214233231 n room: N2 5-7 n http://web.tecnico.ulisboa.pt/andreas.wichert/ 1
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? 2
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/ 3
What is LISP? n Conditionals - if-then-else construct n A function type - functions are first-class objects n Recursion n Typed values rather than typed variables n 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 1920s n Polish notation, also known as Polish prefix notation or simply prefix notation 4
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-toone representation for the same 5
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 6
Literature n Practical Common Lisp, Peter Seibel, 2005, Apress, versaõ online n ANSI Common Lisp, Paul Graham, 1995, Prentice Hall, versaõ online n Introduc aõ a Linguagem Lisp, Antońio Leitaõ, 1995, Instituto Superior Tećnico 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) 7
n Conrad Barski Conrad Barski, M.D. 8
n Dana Nau n Introduction to Lisp / AI material n https://www.cs.umd.edu/users/nau/ We will use Common lisp J n 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)). 9
Installing Common Lisp n CLISP n Download a CLISP installer from n http://clisp.cons.org/. Running clisp 10
Clozure Common Lisp n 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 11
Evaluating >1 1 >pi 3.1415926535897932385L0 > t T > nil NIL 12
Symbols n Common Lisp symbols are typically made up of letters, numbers, and characters like + - / * = < >?! _ n ice9, my-killer-app27, and even -- <<==>>--. 13
Numbers > (+ 1 1.0) 2.0 > (expt 53 53) 2435684816502271213247760652010472551853345312 86856408445051308795767206091502 23301256150373 > (/ 4 6) 2/3 > (/ 4.0 6) 0.6666667 > (- 5 6 7) -8 Why? Evaluates from left >(- (- 5 6) 7) -8 14
Strings (princ "Tutti Frutti") Tutti Frutti "Tutti Frutti" n The text appears twice n 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." 15
Code Mode n > (expt 2 3) n 8 n This calculates 2^3 = 8 n It does this by calling expt, which computes an exponent. 16
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) 17
But (Why? Read the error message) > ( 1 2 3) *** - EVAL: 1 is not a function name; try using a symbol instead The following restarts are available: USE-VALUE :R1 Input a value to be used instead. ABORT :R2 Abort debug loop ABORT :R3 Abort debug loop ABORT :R4 Abort debug loop ABORT :R5 Abort main loop Predicates 18
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)??? 19
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 20
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) 21
Lists Dot notation 22
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) 23
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 24
Nested Lists n Lists can contain other lists. '(cat (duck bat) ant) >(car '((peas carrots tomatoes) (pork beef chicken))) (PEAS CARROTS TOMATOES) 25
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 26
> (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 27
Defining Global Variables 28
Local Variables Editor 29
To load / compile n (load <nome-ficheiro>) n (load projecto.lisp ) n (compile-file "projecto") n (load "projecto") 30
31