Presentation is loading. Please wait.

Presentation is loading. Please wait.

66 2210 - Programming in Lisp; Instructor: Alok Mehta1 66-2210-01 Programming in Lisp Introduction to Lisp.

Similar presentations


Presentation on theme: "66 2210 - Programming in Lisp; Instructor: Alok Mehta1 66-2210-01 Programming in Lisp Introduction to Lisp."— Presentation transcript:

1 66 2210 - Programming in Lisp; Instructor: Alok Mehta1 66-2210-01 Programming in Lisp Introduction to Lisp

2 66 2210 - Programming in Lisp; Instructor: Alok Mehta2 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”

3 66 2210 - Programming in Lisp; Instructor: Alok Mehta3 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

4 66 2210 - Programming in Lisp; Instructor: Alok Mehta4 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)’

5 66 2210 - Programming in Lisp; Instructor: Alok Mehta5 Calling Lisp Procedures  Lisp procedure calls are symbolic expressions  Represented using lists (like everything else)  Format of a Lisp procedure call – ( …)  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

6 66 2210 - Programming in Lisp; Instructor: Alok Mehta6 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

7 66 2210 - Programming in Lisp; Instructor: Alok Mehta7 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

8 66 2210 - Programming in Lisp; Instructor: Alok Mehta8 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)

9 66 2210 - Programming in Lisp; Instructor: Alok Mehta9 Setf Assigns Variables  Setf (SET Field) 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 **

10 66 2210 - Programming in Lisp; Instructor: Alok Mehta10 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)

11 66 2210 - Programming in Lisp; Instructor: Alok Mehta11 Lists are like boxes; NIL=Empty G H C D A B F IJ  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) E

12 66 2210 - Programming in Lisp; Instructor: Alok Mehta12 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

13 66 2210 - Programming in Lisp; Instructor: Alok Mehta13 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

14 66 2210 - Programming in Lisp; Instructor: Alok Mehta14 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

15 66 2210 - Programming in Lisp; Instructor: Alok Mehta15 Setf Revisited  Setf  Format (setf …)  Example > (setf x 0 y 0 z 2) 2  Returns –the value of the last element  Side effects –assigns values for symbols (or variables),, … –the symbol then becomes an atom that evaluates the value assigned to it > x 0

16 66 2210 - Programming in Lisp; Instructor: Alok Mehta16 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 B C A alist Contents of Address Register (CAR) = Old name for “First” Contents of Decrement portion of Register (CDR) = Old name for “Rest” Cons Cell

17 66 2210 - Programming in Lisp; Instructor: Alok Mehta17 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)) ABCDEF

18 66 2210 - Programming in Lisp; Instructor: Alok Mehta18 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

19 66 2210 - Programming in Lisp; Instructor: Alok Mehta19 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)

20 66 2210 - Programming in Lisp; Instructor: Alok Mehta20 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)

21 66 2210 - Programming in Lisp; Instructor: Alok Mehta21 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

22 66 2210 - Programming in Lisp; Instructor: Alok Mehta22 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 ) > (setf i (complex 0 1)) #C(0 1) > (* i i)

23 66 2210 - Programming in Lisp; Instructor: Alok Mehta23 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

24 66 2210 - Programming in Lisp; Instructor: Alok Mehta24 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)


Download ppt "66 2210 - Programming in Lisp; Instructor: Alok Mehta1 66-2210-01 Programming in Lisp Introduction to Lisp."

Similar presentations


Ads by Google