joshua eckroth [email protected]. the plan 1.review some functions. 2.write more functions....

93
Joshua Eckroth [email protected]

Upload: silas-bunting

Post on 28-Mar-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Joshua Eckroth

[email protected]

Page 2: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 3: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 4: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

The Plan

1. Review some functions.2. Write more functions.3. Consider the nature of recursion.4. Look at the call stack.5. Discover tail-call optimization.

Page 5: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

L =

(first L)

(rest L)

Page 6: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

L =

(cons 'a L)

'a

Page 7: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Review: Length of L

(define (length L) (cond [(null? L) 0] [else (+ 1 (length (rest L)))]))

Page 8: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Review: Is e a member of L?

(define (member e L) (cond [(null? L) #f] [(equal? e (first L)) #t] [else (member e (rest L))]))

Page 9: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Review: Remove first e from L

(define (remove e L) (cond [(null? L) '()] [(equal? e (first L)) (rest L)] [else (cons (first L) (remove e (rest L)))]))

Page 10: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Task: Reverse L

(define (reverse L) (cond [__________ __________] [________ _______________________]))

Page 11: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Task: Reverse L

(define (reverse L) (cond [(null? L) '()] [else (append (reverse (rest L)) (list (first L)))]))

Page 12: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Task: Write the “range”function

(define (range lo hi) (cond [_________ ___________] [______ ___________________________]))

Page 13: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Task: Write the “range”function

(define (range lo hi) (cond [(> lo hi) '()] ;; or >= [else (cons lo (range (+ 1 lo) hi))]))

Page 14: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Recursion in “nature.”

Page 15: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A list is the empty list (null),OR,

a “first” value,followed by a list (“rest”).

Page 16: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

0 = {} n + 1 = n U {n}

1 = {0} = {{}}2 = {0, 1} = {{}, {{}}}3 = {0, 1, 2} = {{}, {{}}, {{}, {{}}}}

The Natural Numbers

Page 17: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

'')'( bgafbgaf

'')'( fggffg

))(()( xghxf

)('))((')(' xgxghxf

Derivatives

Sum rule:Product rule:

Chain rule:If,Then,

Page 18: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

def foobar(x): if x < 10: for y in baz: while x > 0: if z == y: print “Too much nesting!”

(define (foobar x) (if (< x 10) (for ([y baz]) (do () (<= x 0) (if (= z y) (display “Too much nesting!”))))))

Languages

Page 19: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

I ate a banana.

I know that I wishI ate a banana.

I wish I ate a banana.

Susan’s brother’s wife’ssister’s cat’s favorite toy…

Page 20: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

… language makes infinite use

of finite means …

- Wilhelm von Humboldt

Page 21: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

“[T]he only reason language needs to be recursive is because its function is to express recursive thoughts. If there were not any recursive thoughts, the means of expression would not need recursion either.”

- Pinker & Jackendoff, “The faculty of language: What’s special about it?” Cognition, 2005, 95(2), pp. 201-236

Page 22: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

The thinker thinks of thinking of thinking.

Corballis, The Recursive Mind: The Origins of Human Language, Thought, and Civilization, Princeton University Press, 2011

Page 23: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

http://www.cs.yale.edu/quotes.html

More Perlisisms:

Recursion is the root of computation since it trades description for time.

- Alan Perlis

Page 24: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

.0 and 0 if

0 and 0 if

0 if

))1,(,1(

)1,1(

1

),(

nm

nm

m

nmAmA

mA

n

nmA

Ackermann function

Looks harmless!

Page 25: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Ackermann function(define (ackermann m n) (cond [(= m 0) (+ 1 n)] [(= n 0) (ackermann (- m 1) 1)] [else (ackermann (- m 1) (ackermann m (- n 1)))]))

Page 26: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A(2, 2) =

Page 27: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A(2, 2) = 9

Page 28: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A(2, 2) = 9

I count 27 function calls.

Page 29: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A(3, 2) =

Page 30: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A(3, 2) = 29

Page 31: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A(3, 2) = 29

540 calls!

Page 32: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A(4, 2) =

Page 33: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A(4, 2) = 20035299304068464649790723515602557504478254755693454734

Page 34: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 35: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Why does recursionget a bad rap?

Page 36: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Stack frames

Page 37: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 38: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 39: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 40: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 41: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 42: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 43: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 44: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 45: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 46: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 47: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Task: “Flatten” L

(define (flatten L) (cond [_________ ____] [______________ (append ____________________ ____________________)] [______ ________________________________ ]))

Page 48: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Task: “Flatten” L

(define (flatten L) (cond [(null? L) '()] [(list? (first L)) (append (flatten (first L)) (flatten (rest L)))] [else (cons (first L) (flatten (rest L)))]))

Page 49: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Quiz: Fill in detailsfor flatten’s call stack.

Page 50: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 51: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 52: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 53: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 54: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 55: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 56: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 57: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 58: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 59: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 60: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 61: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 62: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 63: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 64: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 65: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 66: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 67: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 68: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 69: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 70: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 71: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 72: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 73: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Tail-recursion

Sometimes, recursion can beboth fast and expressive.

Page 74: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Quiz: Identify tail-calls.(define (example L) (if (= 1 0) #f (example (rest L))))

(define (bloop a b c) (cond [(bloop 1 2 3) (bloop 3 2 1)] [else (rest (bloop 0 0 0))]))

Page 75: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Quiz: Identify tail-calls.(define (example L) (if (= 1 0) #f (example (rest L))))

(define (bloop a b c) (cond [(bloop 1 2 3) (bloop 3 2 1)] [else (rest (bloop 0 0 0))]))

Page 76: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Quiz: Identify tail-calls.(define (example L) (if (= 1 0) #f (example (rest L))))

(define (bloop a b c) (cond [(bloop 1 2 3) (bloop 3 2 1)] [else (rest (bloop 0 0 0))]))

Page 77: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Review: Length of L

(define (length L) (cond [(null? L) 0] [else (+ 1 (length (rest L)))]))

Page 78: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A tail-recursive length(define (lengthT L Acc) (cond [(null? L) ________] [else (lengthT (rest L) ____________)]))

Page 79: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A tail-recursive length(define (lengthT L Acc) (cond [(null? L) Acc] [else (lengthT (rest L) (+ 1 Acc)]))

(define (length L) (length L 0))

Page 80: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 81: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 82: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 83: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 84: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 85: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 86: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 87: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the
Page 88: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Review: Remove first e from L

(define (remove e L) (cond [(null? L) '()] [(equal? e (first L)) (rest L)] [else (cons (first L) (remove e (rest L)))]))

Page 89: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A tail-recursive remove(define (removeT e L Acc) (cond [(null? L) _______] [(equal? (first L) e) __________________________] [else (remove e (rest L) ______________________]))

(define (remove e L) (remove e L '()))

Page 90: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

A tail-recursive remove(define (removeT e L Acc) (cond [(null? L) Acc] [(equal? (first L) e) (append Acc (rest L))] [else (remove e (rest L) (cons (first L) Acc))]))

(define (remove e L) (remove e L '()))

Page 91: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Quiz: Write reverse andrange with tail-recursion.

Page 92: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Tail-call optimizationkeeps a small stack.

Page 93: Joshua Eckroth joshuaeckroth@gmail.com. The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the

Recursion is the root of computation since it trades description for time, butnot necessarily.