<<

Lisp & Scheme • Dynamic data structures (lists, binary trees) that grow without Lisp (List Processing Language) limit. is one of the oldest • Automatic garbage collection to programming languages still in manage memory. wide use. • Functions are treated as “first class” It was developed in the late 50s values; they may be passed as and early 60s by John arguments, returned as result McCarthy. values, stored in data structures, and created during execution. Its innovations include: • A formal semantics (written in Lisp) • Support of symbolic computations. that defines the meaning of all • A style valid programs. without emphasis on assignments • An Integrated Programming and side-effects. Environment to create, edit and • A naturally recursive programming test Lisp programs. style. • Dynamic (run-time) type checking.

© © CS 538 Spring 2008 99 CS 538 Spring 2008 100

Scheme Atomic (Primitive) Data Types Scheme is a recent dialect of Symbols: Lisp. Essentially the same form as It uses lexical (static) scoping. identifiers. Similar to enumeration values in C and C++. It supports true first-class Very flexible in structure; functions. essentially any sequence of It provides program-level printable characters is allowed; access to control flow via anything that starts a valid continuation functions. number (except + or -) may not start a symbol. Valid symbols include: abc hello-world + <=!

Integers: Any sequence of digits, optionally prefixed with a + or -. Usually unlimited in length.

© © CS 538 Spring 2008 101 CS 538 Spring 2008 102 Reals: String: A floating point number in a A sequence of characters decimal format (123.456) or in delimited by double quotes. exponential format (1.23e45). A Double quotes and backslashes leading sign and a signed must be escaped using a exponent are allowed backslash. For example (-12.3, 10.0e-20). "Hello World" "\"Wow!\"" Rationals: : Rational numbers of the form A single character prefixed by #\. integer/integer (e.g., 1/3 or 9/7) For example, #\a, #\0, #\\, #\#. with an optional leading sign (-1/ Two special characters are 2, +7/8). #\space and #\newline. Complex: Complex numbers of the form Boolean: num+num i or num-num i, where True is represented as # and num is an integer or real number. false is represented as #f. Example include 1+3i, -1.5- 2.5i, 0+1i).

© © CS 538 Spring 2008 103 CS 538 Spring 2008 104

Binary Trees Binary trees are also called C A S-Expressions in Lisp and Scheme. They are of the form A B B C ( item . item ) S-Expressions are built and where item is any atomic value or any S-Expression. For example: accessed using the predefined functions cons, . ( A . B ) (1.2 . "xyz" ) cons builds a new S-Expression ( (A . B ) . C ) from two S-Expressions that ( A . (B . C ) ) represent the left and right children. S-Expressions are linearizations of binary trees: cons(E1,E2) = (E1 . E2) car returns are left subtree of an S-Expression. A B 1.2 "xyz" car (E1 . E2) = E1 cdr returns are right subtree of an S-Expression. cdr (E1 . E2) = E2

© © CS 538 Spring 2008 105 CS 538 Spring 2008 106 Lists Function Calls In Lisp and Scheme lists are a In List and Scheme, function calls special, widely-used form of S- are represented as lists. Expressions. (A B C) means: () represents the empty or null list Evaluate A (to a function) (A) is the list containing A. By definition, (A) ≡ (A . () ) Evaluate B and C (as parameters) (A B) represents the list Call A with B and C as its containing A and B. By definition, parameters (A B) ≡ (A . (B . () ) ) Use the value returned by the call In general, (A B C ... Z) ≡ as the “meaning” of (A B C). (A . (B . (C . ... (Z . () ) ... ))) cons, car and cdr are predefined symbols bound to built-in (A B C )≡ functions that build and access lists and S-Expressions. A Literals (of type integer, real, rational, complex, string, B character and boolean) evaluate to themselves. C()

© © CS 538 Spring 2008 107 CS 538 Spring 2008 108

For example (⇒ means “evaluates Thus (quote (1 2)) doesn’t try to”) to evaluate the list (1 2); it just (cons 1 2) ⇒ (1 . 2) returns it. Since quotation is so often used, (cons 1 () ) ⇒ (1) it may be abbreviated using a (car (cons 1 2)) ⇒ 1 single quote. That is (cdr (cons 1 ())) ⇒ () (quote arg) ≡ 'arg But, Thus (car (1 2)) fails during (car '(a b c)) ⇒ a execution! (cdr '( (A) (B) (C))) ⇒ Why? ( (B) (C) ) The expression (1 2) looks like a (cons 'a '1) ⇒ (a . 1) call, but 1 isn’t a function! We need some way to “quote” But, symbols and lists we don’t want ('cdr '(A B)) fails! evaluated. Why? (quote arg) is a special function that returns its argument unevaluated.

© © CS 538 Spring 2008 109 CS 538 Spring 2008 110 User-defined Functions We can bind values and functions to global symbols The list using the define function. (lambda (args) (body)) The general form is evaluates to a function with (define id object) (args) as its argument list and id is not evaluated but object (body) as the function body. is. id is bound to the value No quotes are needed for object evaluates to. (args) or (body). For example, Thus (define pi 3.1415926535) (lambda (x) (+ x 1)) evaluates (define plus1 to the increment function. (lambda (x) (+ x 1)) ) Similarly, (define pi*2 (* pi 2)) ((lambda (x) (+ x 1)) 10) ⇒ Once a symbol is defined, it 11 evaluates to the value it is bound to: (plus1 12) ⇒ 13

© © CS 538 Spring 2008 111 CS 538 Spring 2008 112

Since functions are frequently Conditional Expressions in defined, we may abbreviate Scheme (define id (lambda (args) (body)) ) A predicate is a function that as returns a boolean value. By convention, in Scheme, predicate (define (id args) (body) ) names end with “?” Thus For example, (define (plus1 x) (+ x 1)) number? symbol? equal? null? list? In conditionals, #f is false, and everything else, including #t, is true. The if expression is (if pred E1 E2) First pred is evaluated. Depending on its value (#f or not), either E1 or E2 is evaluated (but not both) and returned as the value of the if expression.

© © CS 538 Spring 2008 113 CS 538 Spring 2008 114 For example, Generalized Conditional (if (= 1 (+ 0 1)) 'Yes This is similar to a switch or case: 'No (cond ) (p1 e1) (p2 e2) ... (define (fact n) (else en) (if (= n 0) ) 1 Each of the predicates (p1, p2, ...) (* n (fact (- n 1))) is evaluated until one is true (≠ ) #f). Then the corresponding ) expression (e1, e2, ...) is evaluated and returned as the value of the cond. else acts like a predicate that is always true. Example: (cond ((= a 1) 2) ((= a 2) 3) (else 4) )

© © CS 538 Spring 2008 115 CS 538 Spring 2008 116