slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to LISP PowerPoint Presentation
Download Presentation
Introduction to LISP

Loading in 2 Seconds...

play fullscreen
1 / 34

Introduction to LISP - PowerPoint PPT Presentation


  • 145 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to LISP' - barbie


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

Introduction to LISP

COP 4020 Programming Languages I

Dr. Euripides Montagne

introducing lisp
IntroducingLISP

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

(function_name argument1 argument2 … argument n)

(+ 5 7) = 12

Scribed by Nate Smith

lisp and lambda calculus
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
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
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
Primitive Functions on Numeric Atoms

+ add

can 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
Primitive Functions on Numeric Atoms (continued)

Expressions Values

34 34

(* 3 7) 21

(+ 5 7 3) 15

(- 5 6) -1

(- 24 (* 4 3)) 12

Scribed by Nate Smith

defining functions
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
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
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
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
Output Functions
  • Format

(display <expression>)

(newline)

  • Example

(display “Hello, world!”)

Scribed by Nate Smith

numeric predicates
Numeric Predicates

FunctionMeaning

= Equal

<> Not equal

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

EVEN? Is it an even number?

ODD? Is it an odd number?

ZERO? Is it equal to zero?

Scribed by Nate Smith

boolean values
Boolean Values

ExpressionValue

#T True

#F False

Scribed by Nate Smith

control flow
Control Flow
  • Uses recursion and conditional expressions
  • Two way selection
  • Multiple selection

Scribed by Nate Smith

two way selection
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
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
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
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
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
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 continued1
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
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 continued2
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 continued3
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
Predicate Functions
  • Returns whether two atoms are equal
  • Format

(EQ? atom1 atom2)

  • Example

(EQ? ‘A ‘A) = #T

(EQ? ‘A ‘B) = ()

(EQ? ‘A ‘(A B)) = ()

(EQ? ‘(A B) ‘(A B)) = ()*

* possibly #T depending on the implementation

Scribed by Nate Smith

predicate functions continued
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 continued1
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 continued2
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 continued3
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)))

)

)

Scribed by Nate Smith