1 / 23

For Wednesday

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

nelia
Download Presentation

For Wednesday

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. For Wednesday • Read Chapter 4, sections 1 and 2 • Homework: • Lisp handout 3

  2. Number Predicates • zerop • plusp • evenp • > • < • < and > may take multiple arguments

  3. 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

  4. 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>)

  5. 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)))

  6. 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))

  7. Recursion • Standard method for doing “repetition” in functional languages is recursion • (defun myexpt (m n) (if (zerop n) 1 (* m (myexpt m (- n 1)))))

  8. Recursion Inefficiency • (defun count-elements (l) (if (endp l) 0 (+ 1 (count-elements (rest l)))))

  9. 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

  10. 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))))

  11. 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.

  12. DOTIMES (defun dotimes-expt (m n) (let ((result 1)) (dotimes (count n result) (setf result (* m result)))))

  13. 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)))))))

  14. DO (defun do-expt (m n) (do ((result 1) (exponent n)) ((zerop exponent) result) (setf result (* m result)) (setf exponent (- exponent 1))))

  15. 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>)

  16. 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

  17. 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

  18. 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)))

  19. 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.

  20. 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.

  21. Problem Formulation • Initial state • Goal state • Operators that change the state of the world • Path cost – for when it matters how we get there

  22. Toy Problems • 8-puzzle • N-queens • Peg puzzle • Farmer, wolf, goat and cabbage • Missionaries and cannibals

  23. More Realistic Problems • Route finding • Traveling Salesman Problem • VLSI layout • Robot navigation • Web searching

More Related