4-25-08 cs 331 lisp/scheme 1 lisp/scheme ruth rutter functional and logic programming team p-phunck
TRANSCRIPT
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 11
Lisp/SchemeLisp/Scheme
Ruth RutterRuth Rutter
Functional and Logic ProgrammingFunctional and Logic Programming
Team P-PhunckTeam P-Phunck
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 22
LISPLISP
Functional, not imperativeFunctional, not imperative Created to process lists – LISt ProcessingCreated to process lists – LISt Processing
– Checking mathematical proofsChecking mathematical proofs– Game playing and simulationGame playing and simulation– Graphics and display programmingGraphics and display programming– Artificial intelligenceArtificial intelligence– Integral and differential calculusIntegral and differential calculus– Electrical circuit theoryElectrical circuit theory– Etc.Etc.
Several dialectsSeveral dialects– MacLISP, InterLISP, Franz LISPMacLISP, InterLISP, Franz LISP– Common LISP and SchemeCommon LISP and Scheme
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 33
TimelineTimeline
1958, released at MIT by John McCarthy1958, released at MIT by John McCarthy 1959, LISP 11959, LISP 1 1962, LISP 1.51962, LISP 1.5
– Property lists for atomsProperty lists for atoms– Insertion and deletion of elements from listsInsertion and deletion of elements from lists– Numbers represented with S-expressions instead of atomsNumbers represented with S-expressions instead of atoms– evaleval was developed was developed
Contributed toContributed to– Logo (1968)Logo (1968)– Smalltalk (1971)Smalltalk (1971)– Scheme (1975)Scheme (1975)
1984, Common LISP1984, Common LISP Contributed to CLOS (1989) Common-LISP Object SystemContributed to CLOS (1989) Common-LISP Object System 1994, Common LISP first ANSI standard that incorporated 1994, Common LISP first ANSI standard that incorporated
OOPOOP
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 44
Data “Types”Data “Types”
Atoms – symbols or literalsAtoms – symbols or literals Lists – atoms delimited by parenthesesLists – atoms delimited by parentheses
– (A (B C) D (E (F G)))(A (B C) D (E (F G))) Not really “types” as in imperative Not really “types” as in imperative
languageslanguages Original LISP was typelessOriginal LISP was typeless
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 55
LISP ListsLISP Lists
Sebesta: Concepts of Programming Languages
Copyright © 2007 Addison-Wesley. All rights reserved
Essentially linked lists
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 66
SyntaxSyntax
s_expression = atomic_symbol \s_expression = atomic_symbol \
/ "(" s_expression "."s_expression ")" \/ "(" s_expression "."s_expression ")" \
/ list/ list list = "(" s_expression < s_expression > ")“list = "(" s_expression < s_expression > ")“ atomic_symbol = letter atom_partatomic_symbol = letter atom_part atom_part = empty / letter atom_part / number atom_part = empty / letter atom_part / number
atom_partatom_part letter = "a" / "b" / " ..." / "z“letter = "a" / "b" / " ..." / "z“ number = "1" / "2" / " ..." / "9“number = "1" / "2" / " ..." / "9“
empty = " "empty = " "
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 77
FunctionsFunctions
Expressed the same as dataExpressed the same as data– Lists of parentheses, symbols, literalsLists of parentheses, symbols, literals
Cambridge Polish NotationCambridge Polish Notation– Function( arg1 arg2 )Function( arg1 arg2 )– * x y = TIMES(x y)* x y = TIMES(x y)
No iterationNo iteration– No counter variablesNo counter variables– Recursion insteadRecursion instead
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 88
Primitive FunctionsPrimitive Functions
CARCAR– Contents of the address registerContents of the address register– Returns the first item in a listReturns the first item in a list– CAR(A B C) = ACAR(A B C) = A– Undefined for atomsUndefined for atoms
CDRCDR– Contents of the decrement registerContents of the decrement register– Returns everything but the first itemReturns everything but the first item– CDR(A B C) = (B C)CDR(A B C) = (B C)
CONSCONS– Primitive list constructorPrimitive list constructor– CONS(A (B C D)) = (A B C D)CONS(A (B C D)) = (A B C D)
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 99
QUOTE & CONS ExamplesQUOTE & 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)
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 1010
Predefined FunctionsPredefined Functions
EQUALEQUAL– Boolean function to test equality of S-Boolean function to test equality of S-
expressionsexpressions Arithmetic Functions - detailedArithmetic Functions - detailed
– PLUS, DIFFERENCE, MINUS, TIMES, QUOTIENT, PLUS, DIFFERENCE, MINUS, TIMES, QUOTIENT, REMAINDER, DIVIDEREMAINDER, DIVIDE
– For FLOATS as wellFor FLOATS as well– SQRT, EXPT, RECIP, ADD1, ABSVALSQRT, EXPT, RECIP, ADD1, ABSVAL– ENTIER: returns int <= argENTIER: returns int <= arg
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 1111
Predicate FunctionsPredicate Functions
ArithmeticArithmetic– NUMBERP, FIXP, FLOATP, ONEP, etc.NUMBERP, FIXP, FLOATP, ONEP, etc.– ZEROP NZEROP N
T if N is a numberT if N is a number False otherwiseFalse otherwise
ListList– (NULL L) – T if L is empty or NIL(NULL L) – T if L is empty or NIL– (MEMBER L1 L2) – T if L1 is an element of L2(MEMBER L1 L2) – T if L1 is an element of L2
NOT/NULLNOT/NULL– (NOT P) returns true if P is NIL(NOT P) returns true if P is NIL
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 1212
Functional FormsFunctional Forms Functional CompositionFunctional Composition
– Nonquoted lists are assumed to be functionsNonquoted lists are assumed to be functions– Therefore, parameters must be evaluated first Therefore, parameters must be evaluated first
– applied recursively– applied recursively– Some built into single functionsSome built into single functions
(CAAR x) = (CAR (CAR x))(CAAR x) = (CAR (CAR x)) (CADDAR x) = (CAR (CDR (CDR (CAR x))))(CADDAR x) = (CAR (CDR (CDR (CAR x))))
Apply-to-All Functional FormApply-to-All Functional Form– mapcar: two params, a function and a listmapcar: two params, a function and a list
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 1313
Nameless (LAMBDA) FunctionsNameless (LAMBDA) Functions
Temporarily define functionsTemporarily define functions Syntax:Syntax:
(LAMBDA (LAMBDA varlist bodyvarlist body)) Example: 4Example: 433
(LAMBDA (X Y) (EXPT Y X)) (3 4)(LAMBDA (X Y) (EXPT Y X)) (3 4)
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 1414
Defining FunctionsDefining Functions
Like a permanent LAMBDA functionLike a permanent LAMBDA function Pseudo-functionPseudo-function
– One argument – list of functions to be One argument – list of functions to be defineddefined
Syntax:Syntax:
DEFINE((DEFINE((
(name(name11 (LAMBDA (LAMBDA varlist bodyvarlist body))))
(name(name22 (LAMBDA (LAMBDA varlist bodyvarlist body))))
))))
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 1515
Example: N!Example: N!
n! = UNDEF, n<0n! = UNDEF, n<0
= 1, n=0= 1, n=0
= n * (n-1)!, n>0= n * (n-1)!, n>0
DEFINE((DEFINE((
(FACTORIAL (LAMBDA (N) (COND ((ZEROP N) (FACTORIAL (LAMBDA (N) (COND ((ZEROP N) 1)1)
( T (TIMES N (FACTORIAL (SUB1 N))))))) ))( T (TIMES N (FACTORIAL (SUB1 N))))))) ))
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 1616
ExampleExample Comparing two listsComparing two lists
(DEFINE (equal lis1 lis2)(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)
))))
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 1717
InterpretersInterpreters EVAL – universal function to evaluate any other EVAL – universal function to evaluate any other
functionfunction– Was altered to create the first LISP interpreterWas altered to create the first LISP interpreter
Accidental addition – dynamic scoping until 1975Accidental addition – dynamic scoping until 1975 Online interpreter: (basic)Online interpreter: (basic)
– http://www.joeganley.com/code/jslisp.htmlhttp://www.joeganley.com/code/jslisp.html CLISP most commonly usedCLISP most commonly used
– http://www.clisp.org/ has packages for different OS’shttp://www.clisp.org/ has packages for different OS’s 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/2124http://lambda-the-ultimate.org/node/2124 Ufasoft LispUfasoft Lisp
– http://www.ufasoft.com/lisp/http://www.ufasoft.com/lisp/
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 1818
Common LISPCommon LISP Developed to combine and standardize previous Developed to combine and standardize previous
LISPsLISPs A general purpose and AI languageA general purpose and AI language Supports imperative, functional, and object-Supports imperative, functional, and object-
oriented paradigmsoriented paradigms Has data types includingHas data types including
– Numbers (int, ratio, float, complex, etc.)Numbers (int, ratio, float, complex, etc.)– StringsStrings– ArraysArrays– Symbols (name, value, function, and package)Symbols (name, value, function, and package)
Automatic memory managementAutomatic memory management Includes static and dynamic scopingIncludes static and dynamic scoping
– Declare variables “special” to make dynamicDeclare variables “special” to make dynamic Larger, more complexLarger, more complex
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 1919
SchemeScheme Developed at MIT, mid 1970sDeveloped at MIT, mid 1970s Not exclusively functionalNot exclusively functional
– Supports imperative, functional, and message Supports imperative, functional, and message passingpassing
Functions are first-class entitiesFunctions are first-class entities– Can be values of expressions, elements of lists, Can be values of expressions, elements of lists,
assigned to variables, and passed as parametersassigned to variables, and passed as parameters First LISP to distinguish procedures from First LISP to distinguish procedures from
lambda expressions and symbolslambda expressions and symbols First language to use first class escape First language to use first class escape
proceduresprocedures
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 2020
Scheme IIScheme II
Functions that Build CodeFunctions that Build Code– Replacing functions that only take numeric Replacing functions that only take numeric
atoms, such as Scheme’s + functionatoms, such as Scheme’s + function– Define function to embed “+” into the list, then Define function to embed “+” into the list, then
submit to EVALsubmit to EVAL
(DEFINE (adder lis)(DEFINE (adder lis)
(COND(COND
((NULL? lis) 0)((NULL? lis) 0)
(ELSE (EVAL (CONS ‘+ lis)))(ELSE (EVAL (CONS ‘+ lis)))
))))
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 2121
Modern UsesModern Uses
Viaweb was written in LISP, and later Viaweb was written in LISP, and later became Yahoo! Storebecame Yahoo! Store
ITA Software’s airfare pricing and shopping ITA Software’s airfare pricing and shopping system QPX (used by Orbitz)system QPX (used by Orbitz)
Naughty Dog’s Naughty Dog’s Jak and DaxterJak and Daxter for the for the Playstation 2Playstation 2
Software for RoombaSoftware for Roomba Artificial Intelligence applicationsArtificial Intelligence applications
4-25-084-25-08 CS 331 Lisp/SchemeCS 331 Lisp/Scheme 2222
BibliographyBibliography Hekmatpour, Sharam. Hekmatpour, Sharam. Introduction to LISP and Symbol Introduction to LISP and Symbol
ManipulationManipulation. 1988 Prentice Hall International.. 1988 Prentice Hall International. McCarthy, John. McCarthy, John. History of LispHistory of Lisp. Stanford, 1979.. Stanford, 1979. Sebesta, Robert W. Sebesta, Robert W. Concepts of Programming LanguagesConcepts of Programming Languages. 2008 . 2008
Pearson Education, Inc.Pearson Education, Inc. Touretzky, David S. Touretzky, David S. LISP: A Gentle Introduction to Symbolic LISP: A Gentle Introduction to Symbolic
ComputationComputation. 1984 Harper & Row Publishers.. 1984 Harper & Row Publishers. Weissman, Clark. Weissman, Clark. LISP 1.5 PrimerLISP 1.5 Primer. 1967 Dickenson Publishing . 1967 Dickenson Publishing
Company, Inc.Company, Inc. http://www.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-1_thehttp://www.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-1_the
mes-lisp-scheme.htmlmes-lisp-scheme.html http://clisp.cons.org/propaganda.htmlhttp://clisp.cons.org/propaganda.html http://swiss.csail.mit.edu/projects/scheme/http://swiss.csail.mit.edu/projects/scheme/ http://www.levenez.com/lang/history.html#01http://www.levenez.com/lang/history.html#01 http://www.apl.jhu.edu/~hall/lisp.htmlhttp://www.apl.jhu.edu/~hall/lisp.html http://www.cui.unige.ch/db-research/Enseignement/analyseinfo/http://www.cui.unige.ch/db-research/Enseignement/analyseinfo/
LISP/BNFindex.htmlLISP/BNFindex.html