+ All Categories
Home > Documents > For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

Date post: 26-Dec-2015
Category:
Upload: augustine-murphy
View: 213 times
Download: 1 times
Share this document with a friend
Popular Tags:
23
For Wednesday • Read Chapter 4, sections 1 and 2 • Homework: – Lisp handout 3
Transcript
Page 1: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

For Wednesday

• Read Chapter 4, sections 1 and 2

• Homework:– Lisp handout 3

Page 2: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

Number Predicates

• zerop

• plusp

• evenp

• >

• <

• < and > may take multiple arguments

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

Page 4: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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

Page 5: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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

Page 6: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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

Page 7: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

Recursion

• Standard method for doing “repetition” in functional languages is recursion

• (defun myexpt (m n)(if (zerop n)

1(* m (myexpt m (- n 1)))))

Page 8: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

Recursion Inefficiency

• (defun count-elements (l)(if (endp l)

0(+ 1 (count-elements (rest l)))))

Page 9: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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

Page 10: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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

Page 11: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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.

Page 12: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

DOTIMES

(defun dotimes-expt (m n)

(let ((result 1))

(dotimes (count n result)

(setf result (* m result)))))

Page 13: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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

Page 14: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

DO

(defun do-expt (m n)

(do ((result 1)

(exponent n))

((zerop exponent) result)

(setf result (* m result))

(setf exponent (- exponent 1))))

Page 15: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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

Page 16: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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

Page 17: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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

Page 18: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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

Page 19: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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.

Page 20: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

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.

Page 21: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

Problem Formulation

• Initial state

• Goal state

• Operators that change the state of the world

• Path cost – for when it matters how we get there

Page 22: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

Toy Problems

• 8-puzzle

• N-queens

• Peg puzzle

• Farmer, wolf, goat and cabbage

• Missionaries and cannibals

Page 23: For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

More Realistic Problems

• Route finding

• Traveling Salesman Problem

• VLSI layout

• Robot navigation

• Web searching


Recommended