1 / 31

ICS 313: Programming Language Theory

ICS 313: Programming Language Theory. Module 10: Intro to Lisp. Objectives. Understand the basic syntax of Lisp. Understand the basic evaluation strategy. History: 1960’s. First developed by John McCarthy as a language for symbolic (rather than numeric) computation in 1958, using:

maddox
Télécharger la présentation

ICS 313: Programming Language Theory

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. ICS 313:Programming Language Theory Module 10: Intro to Lisp

  2. Objectives • Understand the basic syntax of Lisp. • Understand the basic evaluation strategy.

  3. History: 1960’s • First developed by John McCarthy as a language for symbolic (rather than numeric) computation in 1958, using: • Algebraic syntax from FORTRAN • Symbol manipulation facilities of IPL • Kleene’s recursive function theory • Lambda calculus as basis for function notation and manipulation • very SLOW: • no compilation of source to machine code • inefficient garbage collector • no optimizing of tail recursion

  4. History: 1970’s • During 1970’s AI and Lisp began to flourish • MacLisp at MIT; Franz LISP (Berkeley); and UCI-LISP (Stanford); InterLisp (Xerox workstations); ZetaLisp for Lisp Machines • Scheme • the Pascal of Lisp • used for teaching and research • Lisp Machines • Designed from chip level for Lisp • Proved Lisp could be fast • Proved that Lisp could be “systems language”

  5. History: 1980’s and 1990’s • Common Lisp developed in 1984 • Common Lisp is to Lisp as PL/I was to FORTRAN and COBOL • Intended to be a unified, commercial, portable, industrial-strength, standard Lisp. • Defines over 750 functions to provide efficient and robust implementations of common Lisp programming constructs • Efficient, highly optimized versions of Common Lisp are now available • CL is THE standard

  6. History: 1990’s • Parallel processing versions of Common Lisp released • Death of Lisp Machine • Stock hardware platforms becoming faster than special purpose hardware • Interoperability with other languages

  7. Why not scheme for 313? • Scheme is not the standard language • High quality implementations of CL exist for Mac, PC, and Unix environments • You will need to learn only a small fraction of the 750 predefined common lisp functions (25-50). • Common Lisp is well integrated with Emacs (which is also written in a dialect of Lisp)

  8. Terminology • Write a program => Define a set of functions • Running a program => Evaluating an expression • Simple syntax: operator precedence issues eliminated • Lists (or S-expressions) are important: • Functions are defined as lists • Lists can be manipulated easily in Lisp • Functions can be manipulated easily

  9. Basic Syntax • Number • examples: 3, 8.9, etc. • Symbol • An object written as a sequence of characters • Symbols are usually manipulated as names that are “bound” to other lisp objects • Symbol FOO might be bound to 4.2 • List • An open parenthesis followed by zero or more Lisp objects followed by a closed parenthesis • () the empty list, or NIL. • (3 (4 5 6) a bc) • ((B (3)))

  10. Basic Syntax (cont.) • Lisp has a richer set of predefined types than many other PLs. • Vector, array, hashtables, string, bitvector, complex, floating point, streams, pathnames • Atoms: • Symbols, numbers, and NIL are called atoms. • NIL is both a list and an atom. • Parentheses: • Extra parentheses are significant in Lisp: • (a b c) is a list of 3 items • ((a b c)) is a list of one item--the item being a list of 3 items.

  11. Programming Note • Efficient programming in Lisp requires automated support for: • Parenthesis matching • Syntax-directed indentation (to determine where missing/extra parentheses are located) • Emacs is ideally suited to Lisp programming.

  12. Evaluation • Programs are lisp objects (i.e. functions) • Evaluation of a lisp object returns a new object. • Evaluation is simply a function called EVAL that maps lisp objects to lisp objects: • EVAL: object => object • we will use the symbol => to represent evaluation • The Lisp interpreter is a loop consisting of: • read a lisp expression • call EVAL on it • print out the result

  13. Basic types of evaluation • Numbers • are “self-evaluating” • example: 5 => 5 • Symbols • Evaluate to their binding or value. • if COLOR has the value 3, then • COLOR => 3 • Alternatively, it could have the value of another symbol • COLOR => RED • Evaluation of an unbound symbol results in an error.

  14. T and Nil • T and NIL are distinguished symbols • Self-evaluating • Cannot be bound to anything else. • NIL represents “false” in logical expressions. • T, or ANYTHING non-NIL, represents true in logical expressions.

  15. Function Application • A list of the form: • (<fn-name> <arg1> <arg2> ...) where: • <fn-name> is (normally) a symbol that is the name of a function, and <arg1> to <argN> are arbitrary lisp objects • First each of the arguments are evaluated, and then the function bound to <fn-name> is applied to the arguments.

  16. Application Examples • (+ 5.3 8) => 13.3 • (+ (+ 4.5 (* 5.7 3)) (- 7 29) (/ 99 23)) => 3.904347 • if X is bound to 23, then (+ X 4) => 27

  17. Function Application (cont.) • Function application can also occur by defining the function directly as a lambda form: • ((lambda (x) (+ 2 x)) 3) => 5 • This form defines an “anonymous” function that has no name and exists only during this particular application. • Common Lisp has dynamic type checking • If the number of arguments or types of arguments are invalid for the function, then an error results (at run-time). • Common lisp uses eager evaluation as its default evaluation mechanism.

  18. Special forms • A list of the form (<specialword> <arg1> <arg2> ..) where: • specialword is one of: • if, let, function, quote, setq, defun, setf, etc. • Special forms are not evaluated in the normal way (ie. eager evaluation): • (if (> x 0) (/ 10 x) x) • Why can’t eager evaluation be used for IF ?

  19. Quote • Quote takes one parameter and the entire expression evaluates to the parameter • (quote (a b c)) => (a b c) • ‘(a b c) => (a b c) • Quote can’t work under eager evaluation. Why? • Quote allows Lisp to represent functions as data: • (+ 1 2) is a program (computes 3 when evaled) • ‘(+ 1 2) is data (the list of three elements) • (+ 2 (+ 1 2)) is 5, but • (+ 2 ‘(+ 1 2)) is an error (why?)

  20. Function Definition • To create a named function, use defun: • (defun <name> (<parameter list>) <documentation string> <forms>) • (defun sumsq (x y) “Returns sum of X and Y squared.” (+ (* x x) (* y y))) • The documentation string is saved in the environment and can be recalled from the editor

  21. Conditional statements • If special form: • (if <test> <then form> <else form>) • (if (= x 0) ‘infinity (/ 10 x))

  22. Conditional statements (cont.) • Cond special form: • (cond <cond-clause>*) • <cond-clause> ::= (<test form> <form>*) • (cond ((= x 1) (print “a small number”)) ((= x 2) (print “a larger number.”)))

  23. Variables • Declared using defvar and defparameter: • (defvar <variable name> <initial value> <documentation string>) • (defvar number-list nil“A list of numbers”) • Assigned values using setf special form: • (setf <place> <new value>) • (setf x 3) => 3 • x => 3 • Imperative Paradigm Alert!

  24. Basic List Processing Functions • list • takes any number args, returns a list: • (list ‘x ‘y ‘z) => (x y z) • (list (list ‘x ‘y) (list ‘x ‘y)) => ((x y) (x y)) • car (or first) • returns the first element of a list • (car (list ‘x ‘y)) => x • cdr (or rest) • returns everything but first element: • (cdr ‘(a b c)) => (b c)

  25. Basic List Processing Functions • append • takes any number of lists as arguments and returns them appended together • (append ‘(a b c) ‘(d e f)) => (a b c d e f) • equal • takes two arguments, and returns T if they are structurally equal or of equal value. • null • returns T if the list is empty, nil otherwise.

  26. A simple program • (defvar balance 0 • “The current balance in the bank.”) • (defun deposit (amount) • “Adds amount to balance.” • (setf balance (+ balance amount))) • (defun withdraw (amount) • “Subtracts amount from balance.” • (setf balance (- balance amount)))

  27. Running the program • > (deposit 100) • 100 • > (deposit 1000) • 1100 • > (withdraw 900) • 200 • > (withdraw 300) • -100

  28. Things to do • Put this program in a file. • Load the program file into Lisp. • Run the program by evaluating forms.

  29. Programming Assignment • Extend the bank balance program as follows: • Disallow negative balances. If a withdrawal leads to a negative balance, don’t do it and leave the balance as it was initially. • Provide a function called “interest” that increases the current balance by 5%. • Support multiple users. Each function should take a symbol which is a user name. That user’s balance should be updated independently of the others. You should use only ONE balance variable (but it should be a list, hint hint.)

  30. Negative balance prevention: (deposit 200)200(withdraw 300)200 Interest: (deposit 200)200(interest)210 Multiple users: (deposit ‘joe 200)200(deposit ‘ann 300)300(withdraw ‘joe 100)100(withdraw ‘ann 100)200 (typo fixed!) Examples

  31. Resources • There are many online Common Lisp tutorials. Use your favorite search engine to find them. • Www.franz.com has a free downloadable Common Lisp environment trial version for Windows. • Allegro Common Lisp is available on uhunix2. Use “help software” to learn how to set it up.

More Related