180 likes | 301 Views
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:
E N D
Common Lisp CS 355
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]
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
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)
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)
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)
Empty Lists • Two representations of the empty list: nil () • The empty list is both an atom and a list * () NIL * nil NIL
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!
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)
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
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
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
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
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
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
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
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