introduction to lisp

Post on 05-Jan-2016

61 Views

Category:

Documents

6 Downloads

Preview:

Click to see full reader

DESCRIPTION

Introduction to LISP. COP 4020 Programming Languages I Dr. Euripides Montagne. Introducing LISP. Lis t P rocessing Language Developed by John McCarthy in the late 1950s McCarthy thought LISP could be used to study computability. - PowerPoint PPT Presentation

TRANSCRIPT

Introduction to LISP

COP 4020 Programming Languages I

Dr. Euripides Montagne

Scribed by Nate Smith

Introducing LISP

List Processing Language

• Developed by John McCarthy in the late 1950s

• McCarthy thought LISP could be used to study computability.

• The main idea was to approach computability from a functional programming standpoint.

Scribed by Nate Smith

Data Types & Structures

• Two fundamental data types– Atoms

• An atom is an alphanumeric string, used either as a variable name or a data item

– Lists• A list is a sequence of elements, where each

element is either an atom or a list

Scribed by Nate Smith

Lists

• Simple lists(A B C D)

• Nested lists(A (B C) D (E (F G)))

• Both these lists contain four elements.

• A, B, C, D, E, F, G are atoms

Scribed by Nate Smith

Lists – Internal Representation

• (A B C D)– Here we have a list

with four elements.

• (A (B C) D (E (F G)))– This list also has four

elements, but two of those are nested lists.

Scribed by Nate Smith

Function Notation

(function_name argument1 argument2 … argument n)

(+ 5 7) = 12

Scribed by Nate Smith

LISP and Lambda Calculus

Lambda calculus was modified to allow the binding of functions to names so that functions could be referenced by other functions and themselves.

(function_name (LAMBDA (arg1 arg2 … arg n) expression))

LISP functions specified in this new notation were called s-expressions (symbolic expressions). An s-expression can be an atom or a list.

Scribed by Nate Smith

Scheme

• A dialect of lisp developed at MIT in the mid-70s (Sussman and Steele 1975)

• Main Features– Small size– Static scope– Treatment of functions as first class entities

• As first class entities, scheme functions can be– values of expressions– elements of lists– assigned to variables– passed as parameters

• Simple syntax and semantics• Well-suited to educational applications

Scribed by Nate Smith

The Interpreter

• In 1965 McCarthy developed a function called “eval” used to evaluate other functions (an interpreter).

• It is a read-evaluate-write infinite loop.• Expressions are interpreted by the function “eval.”• Literals are evaluated to themselves. For

example, if you type in 5, the interpreter will return 5.

• Expressions that are calls to primitive functions are evaluated as follows:– Each parameter is evaluated first.– The primitive function is applied to the parameter values.– The result is displayed.

Scribed by Nate Smith

Primitive Functions on Numeric Atoms

+addcan have zero or more parameters

(returns zero if there are no parameters)

- subtract* multiply

can have zero or more parameters (returns one if there are no parameters)

/ divide

Scribed by Nate Smith

Primitive Functions on Numeric Atoms (continued)

Expressions Values34 34(* 3 7) 21(+ 5 7 3) 15(- 5 6) -1(- 24 (* 4 3)) 12

Scribed by Nate Smith

Defining Functions

• A Lisp program is a collection of function definitions

• Nameless functions include the word “lambda.”

• (lambda (x) (* x x)) Which can be applied as follows (lambda (x) (* x x)) 5 = 25• Here x is the bounded variable within

the lambda expression.

Scribed by Nate Smith

Binding Names to Functions

• Define or defun is used to bind a name to a value or a lambda expression.

• Format(define (function_name parameters) <expression(s)>)

• Example(define (square num) (* num num))

Scribed by Nate Smith

Binding Names to Functions (continued)

• Once the function is evaluated by the interpreter, it can be used as in (square 7) = 49

• Example(define (hypotenuse side1 side2)

(sqrt (+ (square side1) (square side2))))

Scribed by Nate Smith

Binding Names to Values

• (define pi 3.14)(define twopi (* 2 pi))

• Once these two expressions are typed in to the Lisp interpreter, typing pi will return 3.14.

• Names consist of letters, digits, and special characters (except parenthesis) but cannot begin with a digit.

Scribed by Nate Smith

Output Functions

• Format (display <expression>) (newline)

• Example(display “Hello, world!”)

Scribed by Nate Smith

Numeric Predicates

Function Meaning= Equal<> Not equal> Greater than< Less than>= Greater than or equal to<= Less than or equal toEVEN? Is it an even number?ODD? Is it an odd number?ZERO? Is it equal to zero?

Scribed by Nate Smith

Boolean Values

Expression Value#T True#F False

Scribed by Nate Smith

Control Flow

• Uses recursion and conditional expressions

• Two way selection• Multiple selection

Scribed by Nate Smith

Two Way Selection

• Format(if predicate then_expression else_expression)

• Example(define (factorial n)

(if (= n 0)1(* n (factorial (- n 1)))

))

Scribed by Nate Smith

Multiple Selection

• Each parameter is a pair of expressions in which the first is a predicate.

• Format(cond

(predicate1 <expression(s)>)(predicate2 <expression(s)>)…(predicate n <expression(s)>)(else <expression(s)>)

)

Scribed by Nate Smith

Multiple Selection (continued)

• Example(define (compare x y)

(cond((> x y) (display “x greater than y”))((< x y) (display “y greater than x”))(else (display “x equal to y”))

))

Scribed by Nate Smith

List Functions

• To avoid the evaluation of parameters when we are dealing with lists, we use “quote.”

• Format(quote <expression>)

• Example(quote A) = A(quote (A B C)) = (A B C)

• Usually quote is replaced by an apostrophe.‘(A B C) = (A B C)

Scribed by Nate Smith

Primitives for Manipulating Lists

• Car returns the first element from a list.• Format

(car <list>)• Example

(car ‘(A B C)) = A(car ‘((A B) C D)) = (A B)(car ‘A) = error because A is not a list

Scribed by Nate Smith

Primitives for Manipulating Lists (continued)

• Cdr removes the first element in the list and returns the remainder.

• Format(cdr <list>)

• Example(cdr (A B C)) = (B C)(cdr ((A B C) D (E F))) = (D (E F))

Scribed by Nate Smith

Primitives for Manipulating Lists (continued)

• The following example returns the second element from a list.

(define (second list)(car (cdr list))

)

Scribed by Nate Smith

Why CAR and CDR?

• The names refer to the IBM system 704 where car and cdr were used as pointers to registers:

CAR = contents of address register

CDR = contents of decrement register

Scribed by Nate Smith

Primitives for Manipulating Lists (continued)

• Cons is a primitive list constructor. It builds a list from its two arguments, the first being an atom or list, the second usually a list.

• Cons inserts its first argument as the new car of its second argument.

• Format(cons <atom or list> <list>)

Scribed by Nate Smith

Primitives for Manipulating Lists (continued)

• Example(cons ‘A ‘()) = (A)(cons ‘A ‘(B C)) = (A B C)(cons ‘() ‘(A B)) = (() A B)(cons ‘(A B) ‘(C D)) = ((A B) C D)

Scribed by Nate Smith

Predicate Functions

• Returns whether two atoms are equal• Format

(EQ? atom1 atom2)• Example

(EQ? ‘A ‘A) = #T(EQ? ‘A ‘B) = #F(EQ? ‘A ‘(A B)) = ()(EQ? ‘(A B) ‘(A B)) = ()*

* possibly #T depending on the implementation

Scribed by Nate Smith

Predicate Functions (continued)

• Examines whether a parameter is a list

• Format(LIST? <expression>)

• Example(LIST? ‘(X Y)) = #T(LIST? ‘X) = ()(LIST? ‘()) = #T

Scribed by Nate Smith

Predicate Functions (continued)

• Examines whether a given list is empty• Format

(NULL? list)• Example

(NULL? ‘(A B)) = ()(NULL? ‘()) = #T(NULL? ‘A) = ()(NULL? ‘(())) = #T

Scribed by Nate Smith

Predicate Functions (continued)

• Whether an entity is a member of a given list

• Format(MEMBER entity list)

• Example(MEMBER ‘B ‘(A B C)) = #T (MEMBER ‘B ‘(A C D E)) = #F

Scribed by Nate Smith

Predicate Functions (continued)

• The member function is defined as follows:(define (member atom list)

(cond((null? List) #F)((eq? atom (car list)) #T)(else (member atom (cdr list)))

))

top related