Presentation is loading. Please wait.

Presentation is loading. Please wait.

4-25-08 CS 331 Lisp/Scheme 1 Lisp/Scheme Ruth Rutter Functional and Logic Programming Team P-Phunck.

Similar presentations


Presentation on theme: "4-25-08 CS 331 Lisp/Scheme 1 Lisp/Scheme Ruth Rutter Functional and Logic Programming Team P-Phunck."— Presentation transcript:

1 4-25-08 CS 331 Lisp/Scheme 1 Lisp/Scheme Ruth Rutter Functional and Logic Programming Team P-Phunck

2 4-25-08CS 331 Lisp/Scheme2 LISP Functional, not imperative Functional, not imperative Created to process lists – LISt Processing Created to process lists – LISt Processing –Checking mathematical proofs –Game playing and simulation –Graphics and display programming –Artificial intelligence –Integral and differential calculus –Electrical circuit theory –Etc. Several dialects Several dialects –MacLISP, InterLISP, Franz LISP –Common LISP and Scheme

3 4-25-08CS 331 Lisp/Scheme3 Timeline 1958, released at MIT by John McCarthy 1958, released at MIT by John McCarthy 1959, LISP 1 1959, LISP 1 1962, LISP 1.5 1962, LISP 1.5 –Property lists for atoms –Insertion and deletion of elements from lists –Numbers represented with S-expressions instead of atoms –eval was developed Contributed to Contributed to –Logo (1968) –Smalltalk (1971) –Scheme (1975) 1984, Common LISP 1984, Common LISP Contributed to CLOS (1989) Common-LISP Object System Contributed to CLOS (1989) Common-LISP Object System 1994, Common LISP first ANSI standard that incorporated OOP 1994, Common LISP first ANSI standard that incorporated OOP

4 4-25-08CS 331 Lisp/Scheme4 Data “Types” Atoms – symbols or literals Atoms – symbols or literals Lists – atoms delimited by parentheses Lists – atoms delimited by parentheses –(A (B C) D (E (F G))) Not really “types” as in imperative languages Not really “types” as in imperative languages Original LISP was typeless Original LISP was typeless

5 4-25-08CS 331 Lisp/Scheme5 LISP Lists Sebesta: Concepts of Programming Languages Copyright © 2007 Addison-Wesley. All rights reserved Essentially linked lists

6 4-25-08CS 331 Lisp/Scheme6 Syntax s_expression = atomic_symbol \ s_expression = atomic_symbol \ / "(" s_expression "."s_expression ")" \ / "(" s_expression "."s_expression ")" \ / list / list list = "(" s_expression ")“ list = "(" s_expression ")“ atomic_symbol = letter atom_part atomic_symbol = letter atom_part atom_part = empty / letter atom_part / number atom_part atom_part = empty / letter atom_part / number atom_part letter = "a" / "b" / "..." / "z“ letter = "a" / "b" / "..." / "z“ number = "1" / "2" / "..." / "9“ number = "1" / "2" / "..." / "9“ empty = " " empty = " "

7 4-25-08CS 331 Lisp/Scheme7 Functions Expressed the same as data Expressed the same as data –Lists of parentheses, symbols, literals Cambridge Polish Notation Cambridge Polish Notation –Function( arg1 arg2 ) –* x y = TIMES(x y) No iteration No iteration –No counter variables –Recursion instead

8 4-25-08CS 331 Lisp/Scheme8 Primitive Functions CAR CAR –Contents of the address register –Returns the first item in a list –CAR(A B C) = A –Undefined for atoms CDR CDR –Contents of the decrement register –Returns everything but the first item –CDR(A B C) = (B C) CONS CONS –Primitive list constructor –CONS(A (B C D)) = (A B C D)

9 4-25-08CS 331 Lisp/Scheme9 QUOTE & CONS Examples QUOTE returns unevaluated argument (QUOTE X) = X Shorthand = ‘X CONS Examples (CONS ‘A ‘()) = (A) (CONS ‘A ‘(B C)) = (ABC) (CONS ‘() ‘(A B)) = (()A B) (CONS ‘(A B) ‘(C D)) = ((A B) C D)

10 4-25-08CS 331 Lisp/Scheme10 Predefined Functions EQUAL EQUAL –Boolean function to test equality of S-expressions Arithmetic Functions - detailed Arithmetic Functions - detailed –PLUS, DIFFERENCE, MINUS, TIMES, QUOTIENT, REMAINDER, DIVIDE –For FLOATS as well –SQRT, EXPT, RECIP, ADD1, ABSVAL –ENTIER: returns int <= arg

11 4-25-08CS 331 Lisp/Scheme11 Predicate Functions Arithmetic Arithmetic –NUMBERP, FIXP, FLOATP, ONEP, etc. –ZEROP N  T if N is a number  False otherwise List List –(NULL L) – T if L is empty or NIL –(MEMBER L1 L2) – T if L1 is an element of L2 NOT/NULL NOT/NULL –(NOT P) returns true if P is NIL

12 4-25-08CS 331 Lisp/Scheme12 Functional Forms Functional Composition Functional Composition –Nonquoted lists are assumed to be functions –Therefore, parameters must be evaluated first – applied recursively –Some built into single functions  (CAAR x) = (CAR (CAR x))  (CADDAR x) = (CAR (CDR (CDR (CAR x)))) Apply-to-All Functional Form Apply-to-All Functional Form –mapcar: two params, a function and a list

13 4-25-08CS 331 Lisp/Scheme13 Nameless (LAMBDA) Functions Temporarily define functions Temporarily define functions Syntax: Syntax: (LAMBDA varlist body) Example: 4 3 Example: 4 3 (LAMBDA (X Y) (EXPT Y X)) (3 4)

14 4-25-08CS 331 Lisp/Scheme14 Defining Functions Like a permanent LAMBDA function Like a permanent LAMBDA function Pseudo-function Pseudo-function –One argument – list of functions to be defined Syntax: Syntax:DEFINE(( (name 1 (LAMBDA varlist body)) (name 2 (LAMBDA varlist body)) ))

15 4-25-08CS 331 Lisp/Scheme15 Example: N! n! = UNDEF, n<0 = 1, n=0 = n * (n-1)!, n>0 DEFINE(( (FACTORIAL (LAMBDA (N) (COND ((ZEROP N) 1) ( T (TIMES N (FACTORIAL (SUB1 N))))))) ))

16 4-25-08CS 331 Lisp/Scheme16 Example Comparing two lists Comparing two lists (DEFINE (equal lis1 lis2) (COND (COND ((NOT (LIST? lis1)) (EQ? lis1 lis2)) ((NOT (LIST? lis1)) (EQ? lis1 lis2)) ((NOT (LIST? lis2)) #F) ((NOT (LIST? lis2)) #F) ((NULL? lis1) (NULL? lis2)) ((NULL? lis1) (NULL? lis2)) ((NULL? lis2) #F) ((NULL? lis2) #F) ((equal (CAR lis1) (CAR lis2) ((equal (CAR lis1) (CAR lis2) (equal (CDR lis1) (CDR lis2))) (equal (CDR lis1) (CDR lis2))) (ELSE #F) (ELSE #F)))

17 4-25-08CS 331 Lisp/Scheme17 Interpreters EVAL – universal function to evaluate any other function EVAL – universal function to evaluate any other function –Was altered to create the first LISP interpreter Accidental addition – dynamic scoping until 1975 Accidental addition – dynamic scoping until 1975 Online interpreter: (basic) Online interpreter: (basic) –http://www.joeganley.com/code/jslisp.html http://www.joeganley.com/code/jslisp.html CLISP most commonly used CLISP most commonly used –http://www.clisp.org/ has packages for different OS’s http://www.clisp.org/ http://home.att.net/~gobruen/progs/lisp/ http://home.att.net/~gobruen/progs/lisp/ http://home.att.net/~gobruen/progs/lisp/ Pico LISP (minimalist) Pico LISP (minimalist) –http://lambda-the-ultimate.org/node/2124 http://lambda-the-ultimate.org/node/2124 Ufasoft Lisp Ufasoft Lisp –http://www.ufasoft.com/lisp/ http://www.ufasoft.com/lisp/

18 4-25-08CS 331 Lisp/Scheme18 Common LISP Developed to combine and standardize previous LISPs Developed to combine and standardize previous LISPs A general purpose and AI language A general purpose and AI language Supports imperative, functional, and object-oriented paradigms Supports imperative, functional, and object-oriented paradigms Has data types including Has data types including –Numbers (int, ratio, float, complex, etc.) –Strings –Arrays –Symbols (name, value, function, and package) Automatic memory management Automatic memory management Includes static and dynamic scoping Includes static and dynamic scoping –Declare variables “special” to make dynamic Larger, more complex Larger, more complex

19 4-25-08CS 331 Lisp/Scheme19 Scheme Developed at MIT, mid 1970s Developed at MIT, mid 1970s Not exclusively functional Not exclusively functional –Supports imperative, functional, and message passing Functions are first-class entities Functions are first-class entities –Can be values of expressions, elements of lists, assigned to variables, and passed as parameters First LISP to distinguish procedures from lambda expressions and symbols First LISP to distinguish procedures from lambda expressions and symbols First language to use first class escape procedures First language to use first class escape procedures

20 4-25-08CS 331 Lisp/Scheme20 Scheme II Functions that Build Code Functions that Build Code –Replacing functions that only take numeric atoms, such as Scheme’s + function –Define function to embed “+” into the list, then submit to EVAL (DEFINE (adder lis) (COND (COND ((NULL? lis) 0) ((NULL? lis) 0) (ELSE (EVAL (CONS ‘+ lis))) (ELSE (EVAL (CONS ‘+ lis)))))

21 4-25-08CS 331 Lisp/Scheme21 Modern Uses Viaweb was written in LISP, and later became Yahoo! Store Viaweb was written in LISP, and later became Yahoo! Store ITA Software’s airfare pricing and shopping system QPX (used by Orbitz) ITA Software’s airfare pricing and shopping system QPX (used by Orbitz) Naughty Dog’s Jak and Daxter for the Playstation 2 Naughty Dog’s Jak and Daxter for the Playstation 2 Software for Roomba Software for Roomba Artificial Intelligence applications Artificial Intelligence applications

22 4-25-08CS 331 Lisp/Scheme22 Bibliography Hekmatpour, Sharam. Introduction to LISP and Symbol Manipulation. 1988 Prentice Hall International. Hekmatpour, Sharam. Introduction to LISP and Symbol Manipulation. 1988 Prentice Hall International. McCarthy, John. History of Lisp. Stanford, 1979. McCarthy, John. History of Lisp. Stanford, 1979. Sebesta, Robert W. Concepts of Programming Languages. 2008 Pearson Education, Inc. Sebesta, Robert W. Concepts of Programming Languages. 2008 Pearson Education, Inc. Touretzky, David S. LISP: A Gentle Introduction to Symbolic Computation. 1984 Harper & Row Publishers. Touretzky, David S. LISP: A Gentle Introduction to Symbolic Computation. 1984 Harper & Row Publishers. Weissman, Clark. LISP 1.5 Primer. 1967 Dickenson Publishing Company, Inc. Weissman, Clark. LISP 1.5 Primer. 1967 Dickenson Publishing Company, Inc. http://www.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-1_themes- lisp-scheme.html http://www.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-1_themes- lisp-scheme.html http://www.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-1_themes- lisp-scheme.html http://www.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-1_themes- lisp-scheme.html http://clisp.cons.org/propaganda.html http://clisp.cons.org/propaganda.html http://clisp.cons.org/propaganda.html http://swiss.csail.mit.edu/projects/scheme/ http://swiss.csail.mit.edu/projects/scheme/ http://swiss.csail.mit.edu/projects/scheme/ http://www.levenez.com/lang/history.html#01 http://www.levenez.com/lang/history.html#01 http://www.levenez.com/lang/history.html#01 http://www.apl.jhu.edu/~hall/lisp.html http://www.apl.jhu.edu/~hall/lisp.html http://www.apl.jhu.edu/~hall/lisp.html http://www.cui.unige.ch/db- research/Enseignement/analyseinfo/LISP/BNFindex.html http://www.cui.unige.ch/db- research/Enseignement/analyseinfo/LISP/BNFindex.html http://www.cui.unige.ch/db- research/Enseignement/analyseinfo/LISP/BNFindex.html http://www.cui.unige.ch/db- research/Enseignement/analyseinfo/LISP/BNFindex.html


Download ppt "4-25-08 CS 331 Lisp/Scheme 1 Lisp/Scheme Ruth Rutter Functional and Logic Programming Team P-Phunck."

Similar presentations


Ads by Google