240 likes | 371 Views
66-2210-01 Programming in Lisp. Introduction to Lisp. What is Lisp?. Stands for LIS t P rocessing Used for symbol manipulation Interactive (interpreted) Easy to learn Syntax and constructs are extremely simple Helps make computers “Intelligent”. Artificial Intelligence.
E N D
66-2210-01 Programming in Lisp Introduction to Lisp 66 2210 - Programming in Lisp; Instructor: Alok Mehta
What is Lisp? • Stands for LISt Processing • Used for symbol manipulation • Interactive (interpreted) • Easy to learn • Syntax and constructs are extremely simple • Helps make computers “Intelligent” 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Artificial Intelligence • Sample applications • Expert Problem Solvers (e.g. Calculus, Geometry, etc.) • Reasoning, Knowledge Representation • Learning • Education • Intelligent support systems • Natural Language interfaces • Speech • Vision 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Symbolic Expressions • Data and programs are represented uniformly • Expression that describes this course • (course 66221001 • (name (Programming in Lisp)) • (instructor • (name (Alok Mehta)) • (email (mehtaa@cs.rpi.edu))) • (department (Computer Science))) • Expression to add 3 + 2 • (+ 3 2) ; Reverse polish notation! • Symbolic expressions: Atoms and Lists • Atoms - ‘course’, ‘Programming’, ‘+’, ‘7’ • Lists - ‘(+ 3 2)’, ‘(Programming in Lisp)’ 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Calling Lisp Procedures • Lisp procedure calls are symbolic expressions • Represented using lists (like everything else) • Format of a Lisp procedure call • (<procedure-name> <arg1> <arg2> …) • Arithmetic expressions are in “Reverse Polish Notation” • (+ 3 2) ; Returns 5 • Calls the Lisp procedure “+” with arguments “3” and “2” • The return value of the expression is 5 • The “+” procedure can take any number of arguments • (+ 1 2 3 4) ; Returns 10 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Overview of Lisp Syntax • Overview of Lisp Syntax ( Left Parenthesis. Begins a list of items. Lists may be nested. ) Right Parenthesis. Ends a list of items. • (* (+ 3 2) (+ 7 8)) ; Semicolon. Begins a comment (terminates at end of line) • (* (+ 3 2) (+ 7 8)) ; Evaluate ((3+2)*(7+8)) " Double Quote. Surrounds character strings. • "This is a thirty-nine character string." ’ Single (Forward) Quote. Don’t evaluate next expression • '(Programming in Lisp) • Examples • ”(+ 3 2)” ; returns the string "(+ 3 2)” as an atom • (+ 3 2) ; evaluates (+ 3 2) and returns 5 • '(+ 3 2) ; returns the expression (+ 3 2) as a list • Lisp is case-insensitive 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Using Lisp on RCS • Conventions • $ UNIX Prompt • > LISP Interpreter prompt • From a UNIX prompt, start the lisp interpreter • $ gcl • GCL (GNU Common Lisp) Version(2.2) Mon Sep 30 09:45:44 EDT 1996 • Licensed under GNU Public Library License • Contains Enhancements by W. Schelter • > • At the Lisp prompt, type your Lisp Expressions • > (* (+ 3 2) (+ 7 8)) • 75 • > • Lisp expressions return values • Return values can be used in other expressions 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Using Lisp on RCS • Recovering from errors in GCL (:q) • > (+ 4 ’x) • Error: "x" is not of type NUMBER. • Fast links are on: do (si::use-fast-links nil) for debugging • Error signalled by +. • Broken at +. Type :H for Help. • >> :q • Executing lisp commands from a file • > (load "prog1.lsp") • ** Reads and executes the lisp expressions contained in “prog1.lsp” ** • Accessing on-line help • > (help) • Exiting from GCL: “(bye)” or “CTRL-d” • > (bye) 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Setf Assigns Variables • Setf (SETField) assigns variables (side effect) • > (setf a '(+ 5 3)) ; Lisp’s way of saying “a=5+3;” • (+ 5 3) • > (setf b (+ 5 3)) • 8 • Examining variables • > a • (+ 5 3) • > b • 8 • Accessing variables • > (+ 3 b) • 11 • > (+ 3 'b) • ** error ** • > (+ 3 a) • ** error ** 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Cons, Remove, First, Rest • Lists are used to represent knowledge • > (setf complang '(C++ Lisp Java Cobol)) • (C++ LISP JAVA COBOL) • Cons (CONStruct) adds an element to a list • > (setf complang (cons 'Perl complang)) • (PERL C++ LISP JAVA COBOL) • Remove removes an element from a list • > (setf complang (remove 'Cobol complang)) • (PERL C++ LISP JAVA) • First gets the first element of a list • > (first complang) • PERL • Rest gets everything except the first element • > (rest complang) • (C++ LISP JAVA) 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Lists are like boxes; NIL=Empty • Lists are like boxes; they can be nested • ((( A B ) C D ( E ) ( )) ( F ) G H (((I)(J)))) • ‘NIL’ is an empty list • > (setf messy '(((A B) C D (E) ( )) (F) G H (((I)(J)))) ) • (((A B) C D (E) NIL) (F) G H (((I)(J)))) • > (first messy) • ((A B) C D (E) NIL) G H C D F A B E I J 66 2210 - Programming in Lisp; Instructor: Alok Mehta
First, Rest Revisited • First returns the first element of a list • Returns an atom if the first element is an atom • Returns a list if the first element is a list • Rest returns all elements of a list except the first • Always returns a list • Examples • > (first '((a) b)) ; returns (A) • > (first '(a b)) ; returns A • > (first '(a)) ; returns A • > (first '( )) ; returns NIL • > (rest '((a) b)) ; returns (B) • > (rest '(a b)) ; returns (B) • > (rest '(a)) ; returns NIL • > (rest '( )) ; returns NIL 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Getting the second element • Use combinations of first and rest • > (setf abcd '(a b c d)) • (A B C D) • > (first (rest abcd)) • B • > (first '(rest abcd)) • REST ; Quote stops expression from being evaluated! • Or, use second • > (second abcd) • B • third, fourth, … , tenth are also defined 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Exercises • Evaluate • > (first '((a b) (c d))) • > (first (rest (first '((a b) (c d))))) • Use First and Rest to get the symbol PEAR • (apple orange pear grape) • ((apple orange) (pear grapefruit)) • (apple (orange) ((pear)) (((grapefruit)))) • Other useful exercises • Text, 2-2, 2-3, 2-4 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Setf Revisited • Setf • Format • (setf <var1> <value1> <var2> <value2> …) • Example • > (setf x 0 y 0 z 2) • 2 • Returns • the value of the last element • Side effects • assigns values for symbols (or variables) <var1>, <var2>, … • the symbol then becomes an atom that evaluates the value assigned to it • > x • 0 66 2210 - Programming in Lisp; Instructor: Alok Mehta
List storage • Draw List Storage Diagram for • (setf alist '(A (B (C)))) • Explain semantics of functions • first, rest, cons, remove • Draw List Storage diagram for • ((apple orange) (pear grapefruit)) A B C alist C Contents of Address Register (CAR) = Old name for “First” B Contents of Decrement portion of Register (CDR) = Old name for “Rest” A alist Cons Cell 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Append, List • Append • Combines the elements of lists • > (append ’(a b c) ’(d e f)) • (A B C D E F) • List • Creates a new list from its arguments • > (list ’a ’b ’(c)) • (A B (C)) A B C D E F 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Cons, Setf; Push; Pop • Cons has no side effects • > (setf complang '(C++ Lisp Java Cobol)) • (C++ LISP JAVA COBOL) • > (cons ’Perl complang) • (PERL C++ LISP JAVA COBOL) • > complang • (C++ LISP JAVA COBOL) • > (setf complang (cons ’Perl complang)) • (PERL C++ LISP JAVA COBOL) • > complang • (PERL C++ LISP JAVA COBOL) • Push/Pop - Implement a stack data structure • Push - shortcut for adding elements permanently • Pop - shortcut for removing elements permanently • > (push complang ’Fortran) • (FORTRAN PERL C++ LISP JAVA COBOL) • > (pop complang) • (PERL C++ LISP JAVA COBOL 66 2210 - Programming in Lisp; Instructor: Alok Mehta
NthCdr, ButLast, Last • NthCdr - Generalization of Rest • Removes the first N elements; returns rest of list • > (setf complang ’(C++ Java Lisp Cobol)) • (C++ LISP JAVA COBOL) • > (nthcdr 1 complang) ; same as (rest complang) • (LISP JAVA COBOL) • > (nthcdr 2 complang) ; same as (rest (rest complang)) • (JAVA COBOL) • ButLast - Removes the last (n-1) elements • > (butlast complang 2) • (C++ LISP) • Last - Returns a list with all but the last element • This function is analogous to ‘first’ (note: returns a list though) • > (last complang) • (COBOL) 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Length, Reverse, Assoc • Length - Returns the number of top-level elements of a list • > (length ’(1 2 (3 4) 5) • 4 • Reverse - Reverses the top level elements of a list • > (reverse ’(1 2 (3 4) 5) • (5 (3 4) 2 1) ; Note the positions of 3 and 4 • Assoc - Searches sublists for an association (alist) • > (setf sarah ’((height .54) (weight 4.4))) • ((height .54) (weight 4.4)) • > (assoc ’weight sarah) • (weight 4.4) 66 2210 - Programming in Lisp; Instructor: Alok Mehta
T, NIL, Symbols • You can’t reassign the following symbols • T ; True • NIL ; Empty List (also means false) • Symbols can include • letters, digits, + - * / @ $ % ^ & _ = < > ~ . • > (setf mehtaa@cs.rpi.edu+b^2-4*a*c ’funny_variable_name) • FUNNY_VARIABLE_NAME 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Numbers • Lisp defines the following types of numbers • Integers (5, -3) • fixnum (implementation dependent), bignum • Ratios (1/3 -- not the same as .333!) • > (+ 1/3 1/3) ; returns 2/3 • Floating-Point (3.25) • short, single, double, long (all are implementation dependent) • Complex • Format: (complex <real-part> <imaginary-part>) • > (setf i (complex 0 1)) • #C(0 1) • > (* i i) • -1 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Misc Math Functions • (+ x1 x2 …) Returns X1 + X2 + … • (* x1 x2 …), (- x1 x2), (/ x1 x2) Computes -, *, / • (float x) converts “x” to a floating point number • (round x) rounds a number to the closest whole integer • (max x1 x2 …) Returns the maximum of its arguments • (min x1 x2 …) Returns the minimum of its arguments • (expt x1 x2) Computes first argument (x1) raised to the power of the second argument (x2). • (sqrt x) Computes the square root of x • (abs x) Computes the absolute value of x 66 2210 - Programming in Lisp; Instructor: Alok Mehta
Review • Lisp = List Processing • Data and Programs represented using Symbolic Expressions • Atoms, Lists (represented using box analogy or cons cells) • Interpreter functions (load, help, bye) • Misc. math functions (+, -, /, *, sqrt, ...) • Assigning variables (setf) • List manipulation • cons, remove, first, rest, append, list • push, pop • second, third, …, tenth, nthcdr, butlast, last • length, reverse, assoc • T, NIL • Numbers (integers, ratios, floating point, complex) 66 2210 - Programming in Lisp; Instructor: Alok Mehta