CS 342 Lecture -*- Outline -*- * LISP and list processing ad: lists are the "larger values" here most important langauge for AI many new features: g.c., if-expressions lists are universal data strucure effect: understand list processing (not functional programming in sense of functions as objects) ** History and goals 1957 FLPL system based on FORTRAN used for AI (knowledge rep.) if-expressions 1958 McCarthy starts LISP implementation, based on recursion, if-expressions, list primitives 1960 McCarthy publishes universal LISP function (interpreter) made into interpreter on IBM 704 (LISP 1.5) 1984 Common Lisp standard (statically scoped, modules, etc.) ** symbols generated by literals of the form 'a-symbol are values (like numbers as opposed to numerals) look like strings, but not permitting blanks *** examples a-name, this-is-a-symbol-too? - used like _ in Pascal *** literals (constants) formed by quoting names 'a, 'b think of it as a convenient form for "a" 2 ways to think of quotation: 1. turns name into a symbol 2. prevents evaluation of name as a variable difference between x and 'x ** lists either the empty list '() or formed from (cons x lst), where lst is a list compare to set notation, only ordered *** examples students should infer def from following ---------------------- () ; called nil (car cadr caddr) (1 this (another list)) (()) (() ()) (+ 1 2) ---------------------- *** counter-examples --------------------- 1 nope (oops neither () --------------------- *** literals '() '(+ 1 2) extends to end *** notation draw boxcar notation (X) for pointer to '() ** S-expressions union of numbers, symbols, lists *** examples ------------- 1 yes (1 yes) () ((1 yes) (1 yes) () 1 yes) (if (> x y) 3 (+ z 3)) --------------- *** counter-examples --------------- ) oops neither () --------------- ** operations *** lists see page 28 how would you make '(one two three) using cons? draw it p. 30, another way to implement list2? what's the point of list2 (vs. quote)? *** type discrimination use '() for false, 'T for true number? symbol? list? null? (list? '()) ==> () ** control constructs same control operators (set, if, begin, while) best not to use while much usually use recursion and if-expressions ** example programs use type analysis transformation of problem statement equational reasoning *** list processing questions: what's value for '()? how to combine value for car with recursion on cdr? e.g., page 29 (length) page 31 (sieve, remove-multiples) ... compare: interval-list (page 31) for 2 arguments, see page 31 (equal) define merge *** auxilliary functions with extra arguments replace local variables numerical examples put in loop invariant at beginning of while version ------------------- ; functional translation of iterative gcd function (see p. 8) (define gcd (m n) (gcd-aux m n (mod m n))) (define gcd-aux (m n r) ; requires: r = (mod m n) (if (<> r 0) (gcd-aux n r (mod n r)) ; this is a simultaneous assignment n)) (define sigma (m n) (sigma-aux m n m)) (define sigma-aux (i j total) ; requires: total = the sum of {m, m+1, ..., i} (:-) (if (= i j) total (sigma-aux (+ i 1) j (+ total (+ i 1))))) ------------------- auxilliary functions separate error checking (in main function) from real work also perform the loop list example see page 35 discuss problem of global variables, why need temporary. ** alists like environments, tables of key-value associations like dictionaries or records '((LISP good) (C bad)) (LISP good) is an association (mapping) '() is empty alist (mkassoc key value alist) makes new alists from old either inserts new key at end, or updates old value always makes a new list (no mutation) assoc -- observer (lookup) work examples from page 32 different version of association list stuff on page 32 does making names like this make the program (e.g., mkassoc) easier to understand? -------------- (load library.lisp) ; associations, type assoc (define assoc-make (key value) ; : atom, s-exp -> assoc (list2 key value)) (define assoc-key (a) ; : assoc -> atom (car a)) (define assoc-value (a) ; : assoc -> s-exp (cadr a)) ; association lists, type alist (set the-empty-alist '()) ; : alist (define alist-empty? (al) ; : alist -> bool (null? al)) (define alist-first (al) ; : alist -> assoc ; requires: al is not empty (car al)) (define alist-tail (al) ; : alist -> alist ; requires: al is not empty (cdr al)) (define alist-assoc (key al) ; : alist -> s-exp (if (alist-empty? al) '() (if (= key (assoc-key (alist-first al))) (assoc-value (alist-first al)) (alist-assoc key (alist-tail al))))) (define alist-replace (a al) ; : assoc, alist -> alist ; requires: (assoc-key a) is defined ; by al's mapping (alist-replace-aux a al (assoc-key a))) (define alist-replace-aux (a al key) ; : assoc, alist, key -> alist ; requires: (assoc-key a) = key and ; key is defined by al's mapping (if (= key (assoc-key (alist-first al))) (cons a (alist-tail al)) (cons (alist-first al) (alist-replace-aux a (alist-tail al) key)))) (define alist-update (a al) ; : assoc, alist -> alist (if (alist-empty? al) (list1 a) (if (null? (alist-assoc (assoc-key a) al)) (cons a al) (alist-replace a al)))) (define alist-mkassoc (key value al) ; this is like mkassoc ; : atom, s-exp, alist -> alist (alist-update (assoc-make key value) al)) -------------- ** 0-1-infinity Principle A good language definition should only have the numbers 0, 1, infinity e.g., number of characters in an identifier number of elements in a data structure ...