230 likes | 306 Views
For Wednesday. Read Chapter 4, sections 1 and 2 Homework: Lisp handout 3. Number Predicates. zerop plusp evenp > < < and > may take multiple arguments. AND, OR, and NOT. AND and OR work as expected AND returns nil if any of its arguments return nil
E N D
For Wednesday • Read Chapter 4, sections 1 and 2 • Homework: • Lisp handout 3
Number Predicates • zerop • plusp • evenp • > • < • < and > may take multiple arguments
AND, OR, and NOT • AND and OR work as expected • AND returns nil if any of its arguments return nil • OR return nil if all of its arguments return nil • Both are short-circuiting • AND returns value of last argument if non return nil • OR returns value of first non-nil argument • NOT turn non-nil values into nil and nil into t
Selection • (if <test> <then form> <else form>) • (if (symbolp day-or-date) ‘day ‘date) • (when <test> <then form>) • (when (> temp high) (setf high temp) ‘new-record) • (unless <test> <else form>)
COND • (cond (<test 1> <consequent 1-1> …) (<test 2> <consequent 2-1> …) … (<test m> <consequent m-1> …)) • (cond ((eq thing ‘circle) (* pi r r)) ((eq thing ‘sphere) (* 4 pi r r)))
COND cont. • If no test matches, returns nil • Often include a final test clause t (matching anything) • (cond ((> p .75) ‘very-likely) ((> p .5) ‘likely) ((> p .25) ‘unlikely) (t ‘very-unlikely))
Recursion • Standard method for doing “repetition” in functional languages is recursion • (defun myexpt (m n) (if (zerop n) 1 (* m (myexpt m (- n 1)))))
Recursion Inefficiency • (defun count-elements (l) (if (endp l) 0 (+ 1 (count-elements (rest l)))))
Tail Recursion • If we don’t manipulate the result of a recursive call before passing it on, that is called tail recursion • In a tail recursive function, you need not save any but the most recent call (because the result of the last call is the result of all calls) • Good lisps (and prologs) optimize tail-recursive functions/predicates, making them just as efficient as a corresponding loop
Counting More Efficiently • (defun count-elements-cleverly (lis) (count-elements-cleverly-aux lis 0)) • (defun count-elements-cleverly-aux (lis result) (if (endp lis) result (count-elements-cleverly-aux (rest lis) (+ 1 result))))
Iteration • Lisp has several constructs that support iteration. • DOTIMES is the equivalent of a for loop. • DOLIST iterates through a list. • DO and LOOP are general purpose structures.
DOTIMES (defun dotimes-expt (m n) (let ((result 1)) (dotimes (count n result) (setf result (* m result)))))
DOLIST (defun count-outlyers (list-of-elements) (let ((result 0)) (dolist (element list-of-elements result) (when (or (> element boiling) (< element freezing)) (setf result (+ result 1)))))))
DO (defun do-expt (m n) (do ((result 1) (exponent n)) ((zerop exponent) result) (setf result (* m result)) (setf exponent (- exponent 1))))
LOOP • LOOP is simply an infinite loop which can be exited using a return form. • If no return statement is ever executed, the loop is truly infinite. • Form is simply (loop <body forms>)
Transforming Lists • MAPCAR applies a function to each member of a list, producing a list of the results • (mapcar #’oddp ‘(1 2 3))(t nil t) • Can use with your own functions. • Do not have to be predicates
More List Processing • All of the following take a predicate and a list and return a list based on the predicate • Remove-if • Remove-if-not • Count-if • Count-if-not
Explicitly Calling Functions • funcall • (funcall #’append ‘(a b) ‘(x y)) • (funcall #’+ 1 2 3 4 5 6) • (funcall #’first ‘(a b c)) • apply • (apply #’append ‘((a b) (x y))) • (apply #’+ (1 2 3 4 5 6)) • (apply #’first ‘((a b c)))
And More • Common Lisp is a huge language • The handouts are intended to encourage you to try out and explore the language. • This is the end of lecture on Lisp, but I will answer questions at the beginning of each class.
Solving Problems • Getting from the current state of the world to the state we want the world to be in. • May or may not matter how we get there.
Problem Formulation • Initial state • Goal state • Operators that change the state of the world • Path cost – for when it matters how we get there
Toy Problems • 8-puzzle • N-queens • Peg puzzle • Farmer, wolf, goat and cabbage • Missionaries and cannibals
More Realistic Problems • Route finding • Traveling Salesman Problem • VLSI layout • Robot navigation • Web searching