61A Lecture 27 Friday, November 8 Announcements 2 Announcements - - PowerPoint PPT Presentation

61a lecture 27
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 27 Friday, November 8 Announcements 2 Announcements - - PowerPoint PPT Presentation

61A Lecture 27 Friday, November 8 Announcements 2 Announcements Homework 8 due Tuesday 11/12 @ 11:59pm, and it's in Scheme! 2 Announcements Homework 8 due Tuesday 11/12 @ 11:59pm, and it's in Scheme! Project 4 due Thursday 11/21 @


slide-1
SLIDE 1

61A Lecture 27

Friday, November 8

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

Announcements

  • Homework 8 due Tuesday 11/12 @ 11:59pm, and it's in Scheme!

2

slide-4
SLIDE 4

Announcements

  • Homework 8 due Tuesday 11/12 @ 11:59pm, and it's in Scheme!
  • Project 4 due Thursday 11/21 @ 11:59pm, and it's a Scheme interpreter!

2

slide-5
SLIDE 5

Announcements

  • Homework 8 due Tuesday 11/12 @ 11:59pm, and it's in Scheme!
  • Project 4 due Thursday 11/21 @ 11:59pm, and it's a Scheme interpreter!
  • Also, the project is very long. Get started today.

2

slide-6
SLIDE 6

Dynamic Scope

slide-7
SLIDE 7

Dynamic Scope

4

slide-8
SLIDE 8

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope).

4

slide-9
SLIDE 9

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope). Lexical scope: The parent of a frame is the environment in which a procedure was defined.

4

slide-10
SLIDE 10

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope). Lexical scope: The parent of a frame is the environment in which a procedure was defined. Dynamic scope: The parent of a frame is the environment in which a procedure was called.

4

slide-11
SLIDE 11

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope). Lexical scope: The parent of a frame is the environment in which a procedure was defined. Dynamic scope: The parent of a frame is the environment in which a procedure was called. (define f (lambda (x) (+ x y)))

4

slide-12
SLIDE 12

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope). Lexical scope: The parent of a frame is the environment in which a procedure was defined. Dynamic scope: The parent of a frame is the environment in which a procedure was called. (define f (lambda (x) (+ x y))) (define g (lambda (x y) (f (+ x x))))

4

slide-13
SLIDE 13

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope). Lexical scope: The parent of a frame is the environment in which a procedure was defined. Dynamic scope: The parent of a frame is the environment in which a procedure was called. (define f (lambda (x) (+ x y))) (define g (lambda (x y) (f (+ x x)))) (g 3 7)

4

slide-14
SLIDE 14

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope). Lexical scope: The parent of a frame is the environment in which a procedure was defined. Dynamic scope: The parent of a frame is the environment in which a procedure was called. (define f (lambda (x) (+ x y))) (define g (lambda (x y) (f (+ x x)))) (g 3 7) Lexical scope: The parent for f's frame is the global frame.

4

slide-15
SLIDE 15

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope). Lexical scope: The parent of a frame is the environment in which a procedure was defined. Dynamic scope: The parent of a frame is the environment in which a procedure was called. (define f (lambda (x) (+ x y))) (define g (lambda (x y) (f (+ x x)))) (g 3 7) Lexical scope: The parent for f's frame is the global frame. Dynamic scope: The parent for f's frame is g's frame.

4

slide-16
SLIDE 16

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope). Lexical scope: The parent of a frame is the environment in which a procedure was defined. Dynamic scope: The parent of a frame is the environment in which a procedure was called. (define f (lambda (x) (+ x y))) (define g (lambda (x y) (f (+ x x)))) (g 3 7) Lexical scope: The parent for f's frame is the global frame. Dynamic scope: The parent for f's frame is g's frame. Error: unknown identifier: y

4

slide-17
SLIDE 17

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope). Lexical scope: The parent of a frame is the environment in which a procedure was defined. Dynamic scope: The parent of a frame is the environment in which a procedure was called. (define f (lambda (x) (+ x y))) (define g (lambda (x y) (f (+ x x)))) (g 3 7) Lexical scope: The parent for f's frame is the global frame. Dynamic scope: The parent for f's frame is g's frame. Error: unknown identifier: y 13

4

slide-18
SLIDE 18

Dynamic Scope

The way in which names are looked up in Scheme and Python is called lexical scope (or static scope). Lexical scope: The parent of a frame is the environment in which a procedure was defined. Dynamic scope: The parent of a frame is the environment in which a procedure was called. (define f (lambda (x) (+ x y))) (define g (lambda (x y) (f (+ x x)))) (g 3 7) Lexical scope: The parent for f's frame is the global frame. Dynamic scope: The parent for f's frame is g's frame. Error: unknown identifier: y 13 mu Special form to create dynamically scoped procedures

4

slide-19
SLIDE 19

Tail Recursion

slide-20
SLIDE 20

Functional Programming

6

slide-21
SLIDE 21

Functional Programming

All functions are pure functions.

6

slide-22
SLIDE 22

Functional Programming

All functions are pure functions. No re-assignment and no mutable data types.

6

slide-23
SLIDE 23

Functional Programming

All functions are pure functions. No re-assignment and no mutable data types. Name-value bindings are permanent.

6

slide-24
SLIDE 24

Functional Programming

All functions are pure functions. No re-assignment and no mutable data types. Name-value bindings are permanent. Advantages of functional programming:

6

slide-25
SLIDE 25

Functional Programming

All functions are pure functions. No re-assignment and no mutable data types. Name-value bindings are permanent. Advantages of functional programming:

  • The value of an expression is independent of the order in which sub-expressions

are evaluated.

6

slide-26
SLIDE 26

Functional Programming

All functions are pure functions. No re-assignment and no mutable data types. Name-value bindings are permanent. Advantages of functional programming:

  • The value of an expression is independent of the order in which sub-expressions

are evaluated.

  • Sub-expressions can safely be evaluated in parallel or on demand (lazily).

6

slide-27
SLIDE 27

Functional Programming

All functions are pure functions. No re-assignment and no mutable data types. Name-value bindings are permanent. Advantages of functional programming:

  • The value of an expression is independent of the order in which sub-expressions

are evaluated.

  • Sub-expressions can safely be evaluated in parallel or on demand (lazily).
  • Referential transparency: The value of an expression does not change when we

substitute one of its subexpression with the value of that subexpression.

6

slide-28
SLIDE 28

Functional Programming

All functions are pure functions. No re-assignment and no mutable data types. Name-value bindings are permanent. Advantages of functional programming:

  • The value of an expression is independent of the order in which sub-expressions

are evaluated.

  • Sub-expressions can safely be evaluated in parallel or on demand (lazily).
  • Referential transparency: The value of an expression does not change when we

substitute one of its subexpression with the value of that subexpression. But... no for/while statements! Can we make basic iteration efficient? Yes!

6

slide-29
SLIDE 29

Recursion and Iteration in Python

In Python, recursive calls always create new active frames. factorial(n, k) computes: k * n!

7

slide-30
SLIDE 30

Recursion and Iteration in Python

def factorial(n, k): if n == 0: return k else: return factorial(n-1, k*n) In Python, recursive calls always create new active frames. factorial(n, k) computes: k * n!

7

slide-31
SLIDE 31

Recursion and Iteration in Python

def factorial(n, k): if n == 0: return k else: return factorial(n-1, k*n) def factorial(n, k): while n > 0: n, k = n-1, k*n return k In Python, recursive calls always create new active frames. factorial(n, k) computes: k * n!

7

slide-32
SLIDE 32

Recursion and Iteration in Python

Time Space def factorial(n, k): if n == 0: return k else: return factorial(n-1, k*n) def factorial(n, k): while n > 0: n, k = n-1, k*n return k In Python, recursive calls always create new active frames. factorial(n, k) computes: k * n!

7

slide-33
SLIDE 33

Recursion and Iteration in Python

Time Space def factorial(n, k): if n == 0: return k else: return factorial(n-1, k*n) def factorial(n, k): while n > 0: n, k = n-1, k*n return k

Θ(n)

In Python, recursive calls always create new active frames. factorial(n, k) computes: k * n!

7

slide-34
SLIDE 34

Recursion and Iteration in Python

Time Space def factorial(n, k): if n == 0: return k else: return factorial(n-1, k*n) def factorial(n, k): while n > 0: n, k = n-1, k*n return k

Θ(n) Θ(n)

In Python, recursive calls always create new active frames. factorial(n, k) computes: k * n!

7

slide-35
SLIDE 35

Recursion and Iteration in Python

Time Space def factorial(n, k): if n == 0: return k else: return factorial(n-1, k*n) def factorial(n, k): while n > 0: n, k = n-1, k*n return k

Θ(n) Θ(n) Θ(n)

In Python, recursive calls always create new active frames. factorial(n, k) computes: k * n!

7

slide-36
SLIDE 36

Θ(1)

Recursion and Iteration in Python

Time Space def factorial(n, k): if n == 0: return k else: return factorial(n-1, k*n) def factorial(n, k): while n > 0: n, k = n-1, k*n return k

Θ(n) Θ(n) Θ(n)

In Python, recursive calls always create new active frames. factorial(n, k) computes: k * n!

7

slide-37
SLIDE 37

Θ(1)

Recursion and Iteration in Python

Time Space def factorial(n, k): if n == 0: return k else: return factorial(n-1, k*n) def factorial(n, k): while n > 0: n, k = n-1, k*n return k

Θ(n) Θ(n) Θ(n)

In Python, recursive calls always create new active frames. factorial(n, k) computes: k * n!

7

slide-38
SLIDE 38

Tail Recursion

From the Revised7 Report on the Algorithmic Language Scheme: def factorial(n, k): while n > 0: n, k = n-1, k*n return k

8

Θ(1)

Time Space

Θ(n)

slide-39
SLIDE 39

Tail Recursion

From the Revised7 Report on the Algorithmic Language Scheme: "Implementations of Scheme are required to be properly tail-recursive. This allows the execution of an iterative computation in constant space, even if the iterative computation is described by a syntactically recursive procedure." def factorial(n, k): while n > 0: n, k = n-1, k*n return k

8

Θ(1)

Time Space

Θ(n)

slide-40
SLIDE 40

Tail Recursion

From the Revised7 Report on the Algorithmic Language Scheme: "Implementations of Scheme are required to be properly tail-recursive. This allows the execution of an iterative computation in constant space, even if the iterative computation is described by a syntactically recursive procedure." (define (factorial n k) (if (zero? n) k (factorial (- n 1) (* k n)))) def factorial(n, k): while n > 0: n, k = n-1, k*n return k

8

Θ(1)

Time Space

Θ(n)

slide-41
SLIDE 41

Tail Recursion

From the Revised7 Report on the Algorithmic Language Scheme: "Implementations of Scheme are required to be properly tail-recursive. This allows the execution of an iterative computation in constant space, even if the iterative computation is described by a syntactically recursive procedure." (define (factorial n k) (if (zero? n) k (factorial (- n 1) (* k n)))) def factorial(n, k): while n > 0: n, k = n-1, k*n return k

8

Should use resources like

Θ(1)

Time Space

Θ(n)

slide-42
SLIDE 42

Tail Recursion

From the Revised7 Report on the Algorithmic Language Scheme: "Implementations of Scheme are required to be properly tail-recursive. This allows the execution of an iterative computation in constant space, even if the iterative computation is described by a syntactically recursive procedure." (define (factorial n k) (if (zero? n) k (factorial (- n 1) (* k n)))) def factorial(n, k): while n > 0: n, k = n-1, k*n return k How? Eliminate the middleman!

8

Should use resources like

Θ(1)

Time Space

Θ(n)

slide-43
SLIDE 43

Tail Recursion

From the Revised7 Report on the Algorithmic Language Scheme: "Implementations of Scheme are required to be properly tail-recursive. This allows the execution of an iterative computation in constant space, even if the iterative computation is described by a syntactically recursive procedure." (define (factorial n k) (if (zero? n) k (factorial (- n 1) (* k n)))) def factorial(n, k): while n > 0: n, k = n-1, k*n return k How? Eliminate the middleman!

8

Should use resources like

Θ(1)

Time Space

Θ(n)

(Demo)

http://goo.gl/tu9sJW

slide-44
SLIDE 44

Tail Calls

slide-45
SLIDE 45

Tail Calls

10

slide-46
SLIDE 46

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space.

10

slide-47
SLIDE 47

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space. A tail call is a call expression in a tail context:

10

slide-48
SLIDE 48

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space. A tail call is a call expression in a tail context:

  • The last body sub-expression in a lambda expression

10

slide-49
SLIDE 49

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space. A tail call is a call expression in a tail context:

  • The last body sub-expression in a lambda expression
  • Sub-expressions 2 & 3 in a tail context if expression

10

slide-50
SLIDE 50

(define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) )

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space. A tail call is a call expression in a tail context:

  • The last body sub-expression in a lambda expression
  • Sub-expressions 2 & 3 in a tail context if expression

10

slide-51
SLIDE 51

(define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) )

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space. A tail call is a call expression in a tail context:

  • The last body sub-expression in a lambda expression
  • Sub-expressions 2 & 3 in a tail context if expression

10

slide-52
SLIDE 52

(define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) )

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space. A tail call is a call expression in a tail context:

  • The last body sub-expression in a lambda expression
  • Sub-expressions 2 & 3 in a tail context if expression

10

slide-53
SLIDE 53

(define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) )

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space. A tail call is a call expression in a tail context:

  • The last body sub-expression in a lambda expression
  • Sub-expressions 2 & 3 in a tail context if expression
  • All non-predicate sub-expressions in a tail context cond

10

slide-54
SLIDE 54

(define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) )

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space. A tail call is a call expression in a tail context:

  • The last body sub-expression in a lambda expression
  • Sub-expressions 2 & 3 in a tail context if expression
  • All non-predicate sub-expressions in a tail context cond
  • The last sub-expression in a tail context and or or

10

slide-55
SLIDE 55

(define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) )

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space. A tail call is a call expression in a tail context:

  • The last body sub-expression in a lambda expression
  • Sub-expressions 2 & 3 in a tail context if expression
  • All non-predicate sub-expressions in a tail context cond
  • The last sub-expression in a tail context and or or
  • The last sub-expression in a tail context begin

10

slide-56
SLIDE 56

(define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) )

Tail Calls

A procedure call that has not yet returned is active. Some procedure calls are tail

  • calls. A Scheme interpreter should support an unbounded number of active tail calls

using only a constant amount of space. A tail call is a call expression in a tail context:

  • The last body sub-expression in a lambda expression
  • Sub-expressions 2 & 3 in a tail context if expression
  • All non-predicate sub-expressions in a tail context cond
  • The last sub-expression in a tail context and or or
  • The last sub-expression in a tail context begin

10

slide-57
SLIDE 57

Example: Length of a List

11

slide-58
SLIDE 58

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure.

11

slide-59
SLIDE 59

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

slide-60
SLIDE 60

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) )

slide-61
SLIDE 61

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) )

slide-62
SLIDE 62

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) )

slide-63
SLIDE 63

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) Not a tail context

slide-64
SLIDE 64

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) Not a tail context (define (length-tail s)

slide-65
SLIDE 65

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) Not a tail context (define (length-tail s) (define (length-iter s n)

slide-66
SLIDE 66

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) Not a tail context (define (length-tail s) (define (length-iter s n) (if (null? s) n

slide-67
SLIDE 67

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) Not a tail context (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) )

slide-68
SLIDE 68

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) Not a tail context (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) (length-iter s 0) )

slide-69
SLIDE 69

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) Not a tail context (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) (length-iter s 0) )

slide-70
SLIDE 70

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) Not a tail context (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) (length-iter s 0) )

slide-71
SLIDE 71

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) Not a tail context (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) (length-iter s 0) )

slide-72
SLIDE 72

Example: Length of a List

A call expression is not a tail call if more computation is still required in the calling procedure. Linear recursive procedures can often be re-written to use tail calls.

11

(define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) Not a tail context (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) (length-iter s 0) ) Recursive call is a tail call

slide-73
SLIDE 73

Eval with Tail Call Optimization

12

slide-74
SLIDE 74

Eval with Tail Call Optimization

The return value of the tail call is the return value of the current procedure call.

12

slide-75
SLIDE 75

Eval with Tail Call Optimization

The return value of the tail call is the return value of the current procedure call. Therefore, tail calls shouldn't increase the environment size.

12

slide-76
SLIDE 76

Eval with Tail Call Optimization

The return value of the tail call is the return value of the current procedure call. Therefore, tail calls shouldn't increase the environment size.

12

(Demo)

slide-77
SLIDE 77

Tail Recursion Examples

slide-78
SLIDE 78

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-79
SLIDE 79

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-80
SLIDE 80

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-81
SLIDE 81

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-82
SLIDE 82

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-83
SLIDE 83

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-84
SLIDE 84

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-85
SLIDE 85

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-86
SLIDE 86

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-87
SLIDE 87

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-88
SLIDE 88

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-89
SLIDE 89

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-90
SLIDE 90

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-91
SLIDE 91

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-92
SLIDE 92

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-93
SLIDE 93

Which Procedures are Tail Recursive?

14

Which of the following procedures run in constant space?

Θ(1)

;; Compute the length of s. (define (length s) (+ 1 (if (null? s)

  • 1

(length (cdr s))) ) ) ;; Return the nth Fibonacci number. (define (fib n) (define (fib-iter current k) (if (= k n) current (fib-iter (+ current (fib (- k 1))) (+ k 1)) ) ) (if (= 1 n) 0 (fib-iter 1 2))) ;; Return whether s contains v. (define (contains s v) (if (null? s) false (if (= v (car s)) true (contains (cdr s) v)))) ;; Return whether s has any repeated elements. (define (has-repeat s) (if (null? s) false (if (contains? (cdr s) (car s)) true (has-repeat (cdr s))) ) )

slide-94
SLIDE 94

Map and Reduce

slide-95
SLIDE 95

Example: Reduce

16

slide-96
SLIDE 96

Example: Reduce

(define (reduce procedure s start)

16

slide-97
SLIDE 97

Example: Reduce

(define (reduce procedure s start) (reduce * '(3 4 5) 2)

16

slide-98
SLIDE 98

Example: Reduce

(define (reduce procedure s start) (reduce * '(3 4 5) 2) 120

16

slide-99
SLIDE 99

Example: Reduce

(define (reduce procedure s start) (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2))

16

slide-100
SLIDE 100

Example: Reduce

(define (reduce procedure s start) (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2)) (5 4 3 2)

16

slide-101
SLIDE 101

Example: Reduce

(define (reduce procedure s start) (if (null? s) start (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2)) (5 4 3 2)

16

slide-102
SLIDE 102

Example: Reduce

(define (reduce procedure s start) (if (null? s) start (reduce procedure (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2)) (5 4 3 2)

16

slide-103
SLIDE 103

Example: Reduce

(define (reduce procedure s start) (if (null? s) start (reduce procedure (cdr s) (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2)) (5 4 3 2)

16

slide-104
SLIDE 104

Example: Reduce

(define (reduce procedure s start) (if (null? s) start (reduce procedure (cdr s) (procedure start (car s)) ) ) ) (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2)) (5 4 3 2)

16

slide-105
SLIDE 105

Example: Reduce

(define (reduce procedure s start) (if (null? s) start (reduce procedure (cdr s) (procedure start (car s)) ) ) ) (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2)) (5 4 3 2)

16

slide-106
SLIDE 106

Example: Reduce

(define (reduce procedure s start) (if (null? s) start (reduce procedure (cdr s) (procedure start (car s)) ) ) ) (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2)) (5 4 3 2)

16

slide-107
SLIDE 107

Example: Reduce

(define (reduce procedure s start) (if (null? s) start (reduce procedure (cdr s) (procedure start (car s)) ) ) ) (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2)) (5 4 3 2)

16

slide-108
SLIDE 108

Example: Reduce

(define (reduce procedure s start) (if (null? s) start (reduce procedure (cdr s) (procedure start (car s)) ) ) ) Recursive call is a tail call. (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2)) (5 4 3 2)

16

slide-109
SLIDE 109

Example: Reduce

(define (reduce procedure s start) (if (null? s) start (reduce procedure (cdr s) (procedure start (car s)) ) ) ) Recursive call is a tail call. Other calls are not; constant space depends on whether procedure requires constant space. (reduce * '(3 4 5) 2) 120 (reduce (lambda (x y) (cons y x)) '(3 4 5) '(2)) (5 4 3 2)

16

slide-110
SLIDE 110

Example: Map with Only a Constant Number of Frames

17

slide-111
SLIDE 111

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s)

slide-112
SLIDE 112

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s)

slide-113
SLIDE 113

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s) nil

slide-114
SLIDE 114

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s))

slide-115
SLIDE 115

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

slide-116
SLIDE 116

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s))))) (map (lambda (x) (- 5 x)) (list 1 2))

slide-117
SLIDE 117

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil

(map (lambda (x) (- 5 x)) (list 1 2))

slide-118
SLIDE 118

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s

(map (lambda (x) (- 5 x)) (list 1 2))

slide-119
SLIDE 119

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s

(map (lambda (x) (- 5 x)) (list 1 2))

slide-120
SLIDE 120

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s

(map (lambda (x) (- 5 x)) (list 1 2))

slide-121
SLIDE 121

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-122
SLIDE 122

Example: Map with Only a Constant Number of Frames

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-123
SLIDE 123

Example: Map with Only a Constant Number of Frames

(define (map procedure s)

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-124
SLIDE 124

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m)

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-125
SLIDE 125

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s)

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-126
SLIDE 126

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-127
SLIDE 127

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s)

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-128
SLIDE 128

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s))

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-129
SLIDE 129

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s)) m))))

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-130
SLIDE 130

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s)) m)))) (reverse (map-reverse s nil)))

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-131
SLIDE 131

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s)) m)))) (reverse (map-reverse s nil)))

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-132
SLIDE 132

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s)) m)))) (reverse (map-reverse s nil))) (define (reverse s)

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-133
SLIDE 133

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s)) m)))) (reverse (map-reverse s nil))) (define (reverse s) (define (reverse-iter s r)

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-134
SLIDE 134

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s)) m)))) (reverse (map-reverse s nil))) (define (reverse s) (define (reverse-iter s r) (if (null? s)

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-135
SLIDE 135

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s)) m)))) (reverse (map-reverse s nil))) (define (reverse s) (define (reverse-iter s r) (if (null? s) r

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-136
SLIDE 136

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s)) m)))) (reverse (map-reverse s nil))) (define (reverse s) (define (reverse-iter s r) (if (null? s) r (reverse-iter (cdr s)

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-137
SLIDE 137

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s)) m)))) (reverse (map-reverse s nil))) (define (reverse s) (define (reverse-iter s r) (if (null? s) r (reverse-iter (cdr s) (cons (car s) r))))

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-138
SLIDE 138

Example: Map with Only a Constant Number of Frames

(define (map procedure s) (define (map-reverse s m) (if (null? s) m (map-reverse (cdr s) (cons (procedure (car s)) m)))) (reverse (map-reverse s nil))) (define (reverse s) (define (reverse-iter s r) (if (null? s) r (reverse-iter (cdr s) (cons (car s) r)))) (reverse-iter s nil))

17

(define (map procedure s) (if (null? s) nil (cons (procedure (car s)) (map procedure (cdr s)))))

1

Pair

2

Pair

nil s s s 4

Pair

3

Pair

(map (lambda (x) (- 5 x)) (list 1 2))

slide-139
SLIDE 139

General Computing Machines

slide-140
SLIDE 140

An Analogy: Programs Define Machines

19

slide-141
SLIDE 141

An Analogy: Programs Define Machines

Programs specify the logic of a computational device

19

slide-142
SLIDE 142

An Analogy: Programs Define Machines

Programs specify the logic of a computational device factorial

19

slide-143
SLIDE 143

An Analogy: Programs Define Machines

Programs specify the logic of a computational device factorial =

  • factorial

* 1 1 1

19

slide-144
SLIDE 144

An Analogy: Programs Define Machines

Programs specify the logic of a computational device factorial 5 =

  • factorial

* 1 1 1

19

slide-145
SLIDE 145

An Analogy: Programs Define Machines

Programs specify the logic of a computational device factorial 5 120 =

  • factorial

* 1 1 1

19

slide-146
SLIDE 146

Interpreters are General Computing Machine

20

slide-147
SLIDE 147

Interpreters are General Computing Machine

An interpreter can be parameterized to simulate any machine

20

slide-148
SLIDE 148

Interpreters are General Computing Machine

An interpreter can be parameterized to simulate any machine Scheme Interpreter 5 120

(define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1)))))

20

slide-149
SLIDE 149

Interpreters are General Computing Machine

An interpreter can be parameterized to simulate any machine Scheme Interpreter 5 120

(define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1)))))

Our Scheme interpreter is a universal machine

20

slide-150
SLIDE 150

Interpreters are General Computing Machine

An interpreter can be parameterized to simulate any machine Scheme Interpreter 5 120

(define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1)))))

Our Scheme interpreter is a universal machine A bridge between the data objects that are manipulated by our programming language and the programming language itself

20

slide-151
SLIDE 151

Interpreters are General Computing Machine

An interpreter can be parameterized to simulate any machine Scheme Interpreter 5 120

(define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1)))))

Our Scheme interpreter is a universal machine A bridge between the data objects that are manipulated by our programming language and the programming language itself Internally, it is just a set of evaluation rules

20