CSE S. Tanimoto Introduction 1 LISP Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot. -- Eric Raymond* * in How to Become a Hacker, quoted by Paul Graham (see the CSE 341 Syllabus page)
CSE S. Tanimoto Introduction 2 LISP Intentions and Implementations LISP = LISt Processing Intended for processing symbolic information Implementations from noncommercial sites: GNU Common Lisp (GCL) - U of Texas mods to Kyoto Common Lisp CLISP -- developed in Germany. Available implementations from Allegro Common Lisp for Windows, Web edition. Allegro Common Lisp for Linux
CSE S. Tanimoto Introduction 3 LISP: Our Objectives Motivation and History Interactive programming Functional programming List manipulation with recursive functions Polymorphism Language extensibility via macro definitions Closures (compare with objects) Language evolution to support multiple paradigms Lisp on the Web
CSE S. Tanimoto Introduction 4 History of Lisp (continued) John McCarthy, developed the ideas during the Dartmouth Summer Research Project on Artificial Intelligence, First implementation on the IBM 704 John McCarthy published “Recursive functions of symbolic expressions and their computation by machine” in Communications of the Association for Computing Machinery in 1960.
CSE S. Tanimoto Introduction 5 History of Lisp (continued) 1970s: advanced dialects -- MacLisp, InterLisp; Lisp machines (Symbolics, Inc.; Lisp Machines, Inc.; Xerox; Texas Instruments) Late 1970s: Scheme, Portable Standard Lisp, XLISP Common Lisp. Use of Lisp as internal scripting languages: Gnu Emacs, AutoCAD CLOS = Common Lisp Object System ANSI Standard Lisp.
CSE S. Tanimoto Introduction 6 Interacting with Lisp Interaction takes place in a Lisp Listener Window. Lisp runs an endless “ READ-EVAL-PRINT loop” for interacting with the user: READ EVAL PRINT
CSE S. Tanimoto Introduction 7 Interacting with Lisp (continued) > (+ 3 5) 8 > (* 2.5 (+ 2 2)) 10.0 > (setq x 5) 5 > (sqrt x) > (* x x) 25
CSE S. Tanimoto Introduction 8 Lisp’s Syntax Lisp uses one fundamental syntactic construct, the “symbolic expression” or S-expression. Any “atom” such as a number or a symbol, is an S- expression. Any parenthesized list of S-expressions, separated by whitespace, is an S-expression. A functional form is a list of the form (function-name arg1 arg2... argN) This is sometimes called parenthesized prefix form. (+ 3 5) (* 2.5 ( )) (sqrt x)
CSE S. Tanimoto Introduction 9 Parentheses in Lisp Every parenthesis in an S-expression has significance. (a b c) is different from (a (b c)) or ((a b) c). To call a zero-argument function, put its name in parentheses: (read) But not ((read)) The empty list can be denoted ( ) or NIL. Parentheses should always be balanced. (+ 3 (* 4 5) is an incomplete S-expression. Misplaced paren’s are a leading cause of beginners’ Lisp programming errors.
CSE S. Tanimoto Introduction 10 In-Class Exercise Convert the following mathematical expressions into Lisp’s parenthesized prefix form. a b. 5 x + 9 y + z c. 100 w / 7 mod 255 d. P and Q or R e. y = f(x + 1)
CSE S. Tanimoto Introduction 11 Defining a function > (* 5 5 5) 125 > (defun cube (n) (* n n n)) CUBE > (cube 2) 8 > (cube )
CSE S. Tanimoto Introduction 12 Symbolic Values Symbols are like identifiers, but they are commonly used as values as well as variables. > (setq x ’pizza) PIZZA > x PIZZA > (setq pizza ’pepperoni) PEPPERONI > pizza PEPPERONI > (eval x) PEPPERONI
CSE S. Tanimoto Introduction 13 More on Evaluation of Symbols > (setq x ’y) Y > (setq y ’z) Z > (setq z ’x) X > x Y > (eval x) Z > (eval (eval x)) X
CSE S. Tanimoto Introduction 14 Values of Symbols A symbol without a value in the current context is said to be “unbound”. The value of a symbol can be any Lisp object, including a number, another symbol, a functional object, a list, and array, etc. A symbol can have several local (“lexical”) values and one global value. However, symbols belong to “packages”, and two different packages can have symbols with the same name.