Introduction to Lisp Lecture 1 2 January 7








![C++ Grammar ___________________________________ Annex A (informative) Grammar summary [gram] ___________________________________ [gram. key] 1 New C++ Grammar ___________________________________ Annex A (informative) Grammar summary [gram] ___________________________________ [gram. key] 1 New](https://slidetodoc.com/presentation_image_h2/6662301414900ab2d25d725f755794ad/image-9.jpg)


















- Slides: 27
Introduction to Lisp Lecture 1 -2 January 7 th, 1999 CS 250 Lecture 1 2 CS 250: Intro to AI/Lisp
A Brief History of Lisp • Lisp is a family of languages with a long history • Key ideas in Lisp developed – John Mc. Carthy during the 1956 summer at Dartmouth – Primary dialect of Lisp between 1960 and 1965 was Lisp 1. 5 Lecture 1 2 CS 250: Intro to AI/Lisp
More History • Early 1970's two dialects of Lisp – Mac. Lisp – Interlisp • Late 1960's, early 1970's – Lisp machine concept: All Lisp, all the time • Second half of the 1970's: Scheme – Lexical scoping, lexical closures, first-class continuations, and simplified syntax Lecture 1 2 CS 250: Intro to AI/Lisp
The End of History • Late 1970's: OOP impacts Lisp – Flavors, an object-oriented programming system with multiple inheritance, was developed at MIT for the Lisp machine community by Howard Cannon and others – Experience with Smalltalk development of Lisp Object Oriented Programming System (LOOPS) and later Common LOOPS – Eventually grew into CLOS Lecture 1 2 CS 250: Intro to AI/Lisp
Today's Lisp • April 1981: DARPA-sponsored meeting of splintered Lisp community defined Common Lisp • 1986: X 3 J 13 was formed – Technical working group to produce ANSI Common Lisp standard – Goals of this group: portability Lecture 1 2 CS 250: Intro to AI/Lisp
Our Lisp • Digitool took over Macintosh Common Lisp from Apple – Maclab has version 3 – Commonly called Mac Lisp, but no relation • CS machines have ACL • Franz allows free download – ACL Lite (Windows) – ACL for Linux Lecture 1 2 CS 250: Intro to AI/Lisp
Why Use Lisp? • • • Everything's a list! Interactive Symbolic Dynamic Convenient Lecture 1 2 CS 250: Intro to AI/Lisp
Everything's a List! • Data (a b c) • Functions (defun plus (x y) (+ x y)) • Simple syntax: (function-name arg 1 arg 2 …) Lecture 1 2 CS 250: Intro to AI/Lisp
C++ Grammar ___________________________________ Annex A (informative) Grammar summary [gram] ___________________________________ [gram. key] 1 New context dependent keywords are introduced into a program by type def (_dcl. typedef_), namespace (_namespace. def_), class (_class_), enumeration (_dcl. enum_), and template (_temp_) declarations. typedef name: identifier namespace name: original namespace alias original namespace name: identifier namespace alias: identifier class name: identifier template class id enum name: identifier template name: identifier Note that a typedef name naming a class is also a class name (_class. name_). A. 2 Lexical conventions [gram. lex] preprocessing token: header name identifier pp number character literal string literal preprocessing op or punc each non white space character that cannot be one of the above token: identifier keyword literal operator punctuator pp number: digit pp number nondigit pp number e sign pp number E sign pp number. identifier: nondigit identifier digit nondigit: one of _abcdefghijklm nopqrstuvwxyz ABCDEFGHIJKLM NOPQRSTUVWXYZ Lecture 1 2 floating literal: expression list: fractional constant exponent partopt floating suffixopt assignment expression digit sequence exponent part floating suffixopt expression list , assignment expression fractional constant: unary expression: digit sequenceopt. digit sequence postfix expression digit sequence. ++ unary expression exponent part: unary expression e signopt digit sequence unary operator cast expression E signopt digit sequence sizeof unary expression sign: one of sizeof ( type id ) + new expression digit sequence: delete expression digit unary operator: one of literal: digit sequence digit * & + ! ~ integer literal floating suffix: one of new expression: character literal f l F L : : opt new placementopt new type id new initializeropt floating literal string literal: : : opt new placementopt ( type id ) new initializeropt string literal "s char sequenceopt" new placement: boolean literal L"s char sequenceopt" ( expression list ) s char sequence: new type id: integer literal: s char type specifier seq new declaratoropt decimal literal integer suffixopt s char sequence s char A. 5 octal literal integer suffixopt s char: new declarator: hexadecimal literal integer suffixopt any member of the source character set except ptr operator new declaratoropt decimal literal: the double quote ", backslash , or new line character direct new declarator nonzero digit escape sequence direct new declarator: decimal literal digit boolean literal: [ expression ] octal literal: false direct new declarator [ constant expression ] 0 true new initializer: octal literal octal digit ( expression listopt ) hexadecimal literal: A. 3 Basic concepts [gram. basic] delete expression: 0 x hexadecimal digit translation unit: : : opt delete cast expression 0 X hexadecimal digit declaration seqopt : : opt delete [ ] cast expression hexadecimal literal hexadecimal digit cast expression: nonzero digit: one of A. 4 Expressions [gram. expr] unary expression 1 2 3 4 5 6 7 8 9 primary expression: ( type id ) cast expression octal digit: one of literal pm expression: 0 1 2 3 4 5 6 7 this cast expression hexadecimal digit: one of : : identifier pm expression. * cast expression 0 1 2 3 4 5 6 7 8 9 : : operator function id pm expression >* cast expression a b c d e f : : qualified id multiplicative expression: A B C D E F ( expression ) pm expression integer suffix: id expression multiplicative expression * pm expression unsigned suffix long suffixopt multiplicative expression / pm expression long suffix unsigned suffixopt id expression: multiplicative expression % pm expression unsigned suffix: one of unqualified id additive expression: u U qualified id multiplicative expression long suffix: one of additive expression + multiplicative expression l L id expression: additive expression multiplicative expression character literal: unqualified id shift expression: 'c char sequence' qualified id additive expression L'c char sequence' shift expression << additive expression c char sequence: unqualified id: shift expression >> additive expression c char identifier relational expression: c char sequence c char operator function id shift expression c char: conversion function id relational expression < shift expression any member of the source character set except ~ class name relational expression > shift expression the single quote ', backslash , or new line character template id relational expression <= shift expression escape sequence qualified id: relational expression >= shift expression escape sequence: nested name specifier templateopt unqualified id equality expression: simple escape sequence nested name specifier: relational expression octal escape sequence class or namespace name : : nested name specifieropt equality expression == relational expression hexadecimal escape sequence equality expression != relational expression simple escape sequence: one of class or namespace name: and expression: ' " ? \ class name equality expression a b f n r t v namespace name and expression & equality expression A. 6 octal escape sequence: postfix expression: exclusive or expression: octal digit primary expression and expression octal digit postfix expression [ expression ] exclusive or expression ^ and expression octal digit postfix expression ( expression listopt ) inclusive or expression: hexadecimal escape sequence: simple type specifier ( expression listopt ) exclusive or expression x hexadecimal digit postfix expression. templateopt : : opt id expression inclusive or expression | exclusive or expression hexadecimal escape sequence hexadecimal digit postfix expression > templateopt : : opt id expression logical and expression: postfix expression ++ inclusive or expression postfix expression dynamic_cast < type id > ( expression ) static_cast < type id > ( expression ) reinterpret_cast < type id > ( expression ) const_cast < type id > ( expression ) typeid ( type id ) "This summary of C++ syntax is intended to be an aid to comprehension. It is not an exact statement of the language. " 1 This summary of C++ syntax is intended to be an aid to comprehension. It is not an exact statement of the language. In particular, the grammar described here accepts a superset of valid C++ constructs. Disambiguation rules (_stmt. ambig_, _dcl. spec_, _class. member. lookup_) must be applied to distinguish expressions from declarations. Fur ther, access control, ambiguity, and type rules must be used to weed out syntactically valid but meaningless constructs. A. 1 Keywords digit: one of 0123456789 preprocessing op or punc: one of { } [ ] # ## ( ) <: : > <% %> %: %: %: ; : . . . new delete ? : : . . * + * / % ^ & | ~ ! = < > += = *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ , >* > and_eq bitand bitor compl not_eq or or_eq xor_eq CS 250: Intro to AI/Lisp logical and expression && inclusive or expression decl specifier seqopt init declarator listopt ; logical or expression: decl specifier seqopt init declarator listopt ; logical and expression decl specifier: logical or expression || logical and expression storage class specifier conditional expression: type specifier logical or expression function specifier logical or expression ? expression : assignment expression friend assignment expression: typedef conditional expression decl specifier seq: logical or expression assignment operator assignment expression decl specifier seqopt decl specifier throw expression storage class specifier: assignment operator: one of auto = *= /= %= += = >>= <<= &= ^= |= register expression: static assignment expression extern expression , assignment expression mutable constant expression: function specifier: conditional expression inline virtual Statements [gram. stmt] explicit statement: typedef name: labeled statement identifier expression statement type specifier: compound statement simple type specifier selection statement class specifier iteration statement enum specifier jump statement elaborated type specifier declaration statement cv qualifier try block labeled statement: simple type specifier: identifier : statement : : opt nested name specifieropt type name case constant expression : statement char default : statement wchar_t expression statement: bool expressionopt ; short compound statement: int { statement seqopt } long statement seq: signed statement unsigned statement seq statement float selection statement: double if ( condition ) statement void if ( condition ) statement else statement type name: switch ( condition ) statement class name condition: enum name expression typedef name type specifier seq declarator = assignment expression elaborated type specifier: iteration statement: class key : : opt nested name specifieropt identifier while ( condition ) statement enum : : opt nested name specifieropt identifier do statement while ( expression ) ; class key: for ( for init statement conditionopt ; expressionopt ) statement class for init statement: struct expression statement union simple declaration enum name: identifier jump statement: enum specifier: break ; enum identifieropt { enumerator listopt } continue ; enumerator list: return expressionopt ; enumerator definition goto identifier ; enumerator list , enumerator definition declaration statement: enumerator definition: block declaration enumerator = constant expression Declarations [gram. dcl] enumerator: declaration seq: identifier declaration seq declaration namespace name: declaration: original namespace name block declaration namespace alias function definition original namespace name: template declaration identifier linkage specification namespace definition: block declaration: named namespace definition simple declaration unnamed namespace definition asm definition namespace alias definition named namespace definition: using declaration original namespace definition using directive extension namespace definition simple declaration: original namespace definition: namespace identifier { namespace body } ….
Interpreted & interactive • Interpreted • Interactive Lecture 1 2 USER(1): 12 12 USER(2): (+ 12 3) 15 USER(3): (setf Almost-age 31) 31 USER(4): Almost-age 31 USER(5): 'Almost-age ALMOST-AGE USER(6): CS 250: Intro to AI/Lisp
Symbolic • Why do we care about symbols? – Understand human cognition with a language like symbolic processing Lecture 1 2 CS 250: Intro to AI/Lisp
Physical Symbol System Hypothesis "A physical symbol system has the necessary and sufficient means for general intelligent action. " (Newell & Simon 1976) • Physical symbol system – Set of entities called symbols - physical patterns that can occur as components – Expressions (or symbol structures) built of symbols Lecture 1 2 CS 250: Intro to AI/Lisp
Dynamic • Functions are first-class objects – Pass functions as arguments to other functions USER(1): (+ 1 2 3) 6 USER(2): (apply #'+ '(1 2 3)) 6 Lecture 1 2 CS 250: Intro to AI/Lisp
One Function, and a Side of Fries, To Go • Create functions on the fly USER(1): (funcall #'(lambda (x y) (+ x y)) 17 14) 31 • Lisp contains itself: eval Lecture 1 2 CS 250: Intro to AI/Lisp
Name Calling • Lisp remembers function names separately from variable names USER(22): (defun add (x y) (+ x y)) ADD USER(23): (setf add 9) 9 USER(24): add 9 USER(25): #'add #<Interpreted Function ADD> Lecture 1 2 CS 250: Intro to AI/Lisp
Convenient • No memory management - forget about malloc() and new() Not a hard new language you have to learn - a tool created for the kinds of problems you have to solve Lecture 1 2 CS 250: Intro to AI/Lisp
A Few Lisp Features Lecture 1 2 CS 250: Intro to AI/Lisp
Lists and More Lists • Lists are marked with parentheses Just a bunch of numbers 2 3 4 (2 3 4 'Cowboys) A list • Data and functions use the same list structure ('Cowboys 'Cardinals 'Bills 'Dolphins) Function (devastate 'Cardinals 'Cowboys) Lecture 1 2 CS 250: Intro to AI/Lisp
List Structure cdr car (a b c) nil a Lecture 1 2 b CS 250: Intro to AI/Lisp c
Now devastate, Appreciate, Depreciate, Fabricate, Emulate, Evaluate. . . • Default evaluation of lists – Look at all the items after the first (cdr) – Evaluate each item (left to right) – Take the first item in the list (car) as the called function – Pass the evaluated items to the function Lecture 1 2 CS 250: Intro to AI/Lisp
An Example • Start with a list of items (remove 'a '(a b r a c a d a b r a)) USER(2): 'a A USER(3): '(a b r a c a d a b r a) (A B R A C A D A B R. . . ) USER(4): (remove 'a '(a b r a c a d a b r a)) (B R C D B R) Lecture 1 2 CS 250: Intro to AI/Lisp
Dysfunctional Programming • In C or C++, data is transformed m = 4 n = 3 f(x, y) = x 3 + 7 y 2 What's f(m, n)? What is m? n? • In mathematics, functions are evaluated without changing their arguments Lecture 1 2 CS 250: Intro to AI/Lisp
Functional Programming • • Functions don't have side effects Why not? Why might you want side effects? Other functional languages – Haskell, ML, F – Common in compiler research Lecture 1 2 CS 250: Intro to AI/Lisp
Assignment • Hard-core functional programming languages don't have assignment • Lisp uses setf USER(1): (setf Tennis-Player-1 'Sampras) SAMPRAS USER(2): Tennis-Player-1 SAMPRAS Lecture 1 2 CS 250: Intro to AI/Lisp
Do it again! And again, and again. . . • Recursion: A technique for repetition – Example: walking across the room • Write a function that gives the sum of all the numbers from 1 to n Lecture 1 2 CS 250: Intro to AI/Lisp
sum-to in Action (defun sum-to (x sum-so-far) (if (eql x 0) sum-so-far (sum-to (- x 1) (+ x sum-so-far)))) USER(1): (trace sum-to) (SUM-TO) Lecture 1 2 USER(2): (sum-to 4 0) 0: (SUM-TO 4 0) 1: (SUM-TO 3 4) 2: (SUM-TO 2 7) 3: (SUM-TO 1 9) 4: (SUM-TO 0 10) 4: returned 10 3: returned 10 2: returned 10 1: returned 10 0: returned 10 10 CS 250: Intro to AI/Lisp
Types in Stereo Strongly Typed Weakly Typed • Languages: C++, • Languages: Lisp & Java others • Variables have types • Values have types • Static type checking • Typing is optional – Double-check the programmer Lecture 1 2 CS 250: Intro to AI/Lisp