1 / 17

Common Lisp

Common Lisp. CS 355. Interpreted Lisp The Top Level. * (+ 2 2) 4 * (+ 2 2 2) 6 * (/ (* 12 6) (* 4 2)) 9 * (/ 3 0) Arithmetic error DIVISION-BY-ZERO signalled. Restarts: 0: [ABORT] Return to Top-Level. Debug (type H for help) 0]. Evaluation. Lisp expressions have 2 flavors:

lavina
Download Presentation

Common Lisp

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. Common Lisp CS 355

  2. Interpreted LispThe Top Level * (+ 2 2) 4 * (+ 2 2 2) 6 * (/ (* 12 6) (* 4 2)) 9 * (/ 3 0) Arithmetic error DIVISION-BY-ZERO signalled. Restarts: 0: [ABORT] Return to Top-Level. Debug (type H for help) 0]

  3. Evaluation • Lisp expressions have 2 flavors: • Atoms : indivisible entities like integers. • Lists : ordered collection of atoms and/or lists. • Atoms evaluate to themselves. • The elements of a list are enclosed in parentheses and are separated by whitespace, e.g. (+ 2 2) • The evaluation rule for lists: • The first element is the operator and the remaining elements are the arguments (i.e., prefix notation). • If the operator is a function (usual case), then the arguments are evaluated in order which are then passed to the corresponding function. * (+ 1 2 (* 3 4) 5 (- 6 7)) 19

  4. Protecting expressions from being evaluated • Many times we want to create lists that simply hold data -- i.e., we do not want them to be evaluated. • The special operator quote simply returns its argument -- evaluation is suppressed. • Lisp defines ' as an abbreviation to quote. * (quote (+ 1 2 3)) (+ 1 2 3) * '(1 2 3) (1 2 3)

  5. Data • Lisp offers all the usual data types plus some others. * 12 ; integer 12 * 3.1415 ; floating point 3.1415 * "hello world" ; string "hello world" * 1283764918237649182736426734 ; big integer 1283764918237649182736426734 * (/ 3 4) ; evaluates to a rational 3/4 * (sqrt -3) ; evaluates to a complex number #C(0.0 1.7320508)

  6. Symbols • Symbols are words • usually converted to upper case • Lisp is case-insensitive. * 'fred FRED * '(james lee ross) (JAMES LEE ROSS) * '(the list (1 2 3) contains two primes) (THE LIST (1 2 3) CONTAINS TWO PRIMES)

  7. Empty Lists • Two representations of the empty list: nil () • The empty list is both an atom and a list * () NIL * nil NIL

  8. Building lists with list • We can build lists the list function • Since list is a function, its arguments are evaluated in order. * (list 'primes (+ 1 1) (/ 6 2) (+ 2 3) (/ 21 3)) (PRIMES 2 3 5 7) Lisp programs are expressed as Lists!

  9. Building lists with cons • If the 2nd argument is a list, cons returns a new list with the 1st argument added to the front: * (cons '1 '(2 3 4)) (1 2 3 4) * (cons 1 nil) (1) * (cons 1 (cons 2 (cons 3 (cons 4 nil)))) (1 2 3 4)

  10. Extracting from a list with car and cdr • car returns the first element of a list. • cdr returns the rest of the list. * (car '(red green blue)) RED * (cdr '(red green blue)) (GREEN BLUE) * (car (cdr '(red green blue))) GREEN

  11. Truth • nil is "false" • Anything else is considered "true" • The symbol t is the default representation of "true" • Predicates are functions that evaluate to "true" or "false" (Lisp predicates often end with p). * (listp '(billy bob bates)) ; is it a list? T * (listp 3.14) NIL * (null '(billy bob bates)) NIL * (null ()) T

  12. Logical operators • not returns t if its argument is nil. • and and or • Short circuit evaluation: only as many arguments that are needed are evaluated • Macros, not functions (evaluation rule not followed) * (and 1 2 nil) NIL * (and 1 2 3) 3 * (or 1 2 nil) 1 * (or 1 2 (/ 3 0)) 1 * (not (or 1 2 (/ 3 0))) NIL

  13. if conditional • Takes 2 or 3 arguments • test expression • then expression • else expressions (optional) • Not a function (only necessary expressions evaluated). * (if (listp '(a b c)) (+ 2 3 4) (* 5 6)) 9 * (if (listp 27) 'yes 'no) NO * (if (null '(a b c)) 'yep) NIL

  14. Equality • eq: same object (think pointer comparison) • equal: same atoms or same structure • = : numeric comparison • equalp : equal or = • many others * (eq '(a b) '(a b)) NIL * (eq 'a 'a) T * (equal '(a b) '(a b)) T * (equal '(a b) '(a b c)) NIL * (= 3 (/ 6 2)) T

  15. cond, the mother of all conditionals • Like a cascaded if-statement • Each argument must be a list consisting of a condition followed by an expression. • The conditions are evaluated in order until one evaluates to true. When it does, the expression associated with it is evaluated and becomes the result of the cond. *(cond (nil 1) (t 2) (t 3)) 2 *(cond (nil 1) ((listp 'a) 2) ((= 3 (+ 1 1)) 3) (t 4) (t 5) ) 4

  16. Functions • New functions defined with defun. • Takes 3 (or more) arguments: • Name, parameter list, and body (1 or more expressions in body) * (defun our-third (x) (car (cdr (cdr x))) ) OUR-THIRD * (our-third '(a b c d e f)) C

  17. Think recursively • Functional languages like Lisp encourage recursion over iteration • Less side-effects • Often simpler to write * (defun fact (n) (if (<= n 1) 1 (* n (fact (- n 1))) ) ) FACT * (fact 6) 720 * (fact 123) 1214630436702532967576624324188129585545421 7088483382315328918161829235892362167668831 1569606126402021707358352212940477825910915 7041165147218602951990626164673073390741981 4952960000000000000000000000000000

More Related