1 / 34

Introduction to LISP

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.

barbie
Télécharger la présentation

Introduction to LISP

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to LISP COP 4020 Programming Languages I Dr. Euripides Montagne

  2. 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

  3. 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

  4. 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

  5. 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

  6. Function Notation (function_name argument1 argument2 … argument n) (+ 5 7) = 12 Scribed by Nate Smith

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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. Output Functions • Format (display <expression>) (newline) • Example (display “Hello, world!”) Scribed by Nate Smith

  17. 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

  18. Boolean Values ExpressionValue #T True #F False Scribed by Nate Smith

  19. Control Flow • Uses recursion and conditional expressions • Two way selection • Multiple selection Scribed by Nate Smith

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

More Related