66-2210-01 programming in lisp

24
66 2210 - Programming in Lisp; Instr uctor: Alok Mehta 1 66-2210-01 Programming in Lisp Introduction to Lisp

Upload: lark

Post on 05-Jan-2016

47 views

Category:

Documents


3 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta

1

66-2210-01 Programming in Lisp

Introduction to Lisp

Page 2: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 2

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”

Page 3: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 3

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

Page 4: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 4

Symbolic Expressions

Data and programs are represented uniformly Expression that describes this course

(course 66221001 (name (Programming in Lisp)) (instructor (name (Alok Mehta)) (email ([email protected]))) (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)’

Page 5: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 5

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

Page 6: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 6

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

Page 7: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 7

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 1996Licensed under GNU Public Library LicenseContains 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

Page 8: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 8

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 debuggingError 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)

Page 9: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 9

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 **

Page 10: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 10

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)

Page 11: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 11

Lists are like boxes; NIL=Empty

G HC DA B F I J

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

Page 12: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 12

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

Page 13: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 13

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

Page 14: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 14

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

Page 15: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 15

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

> x0

Page 16: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 16

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

alist

Contents of Address Register(CAR) = Old name for “First”

Contents of Decrement portion of Register

(CDR) = Old name for “Rest”

Cons Cell

Page 17: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 17

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

Page 18: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 18

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

Page 19: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 19

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)

Page 20: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 20

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)

Page 21: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 21

T, NIL, Symbols

You can’t reassign the following symbolsT ; TrueNIL ; Empty List (also means false)

Symbols can include– letters, digits, + - * / @ $ % ^ & _ = < > ~ .

> (setf [email protected]+b^2-4*a*c ’funny_variable_name)

FUNNY_VARIABLE_NAME

Page 22: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 22

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

Page 23: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 23

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

Page 24: 66-2210-01 Programming in Lisp

66 2210 - Programming in Lisp; Instructor: Alok Mehta 24

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)