61a lecture 27
play

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 @


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

  2. Tail Recursion From the Revised 7 Report on the Algorithmic Language Scheme: Time Space Θ ( n ) Θ (1) def factorial(n, k): while n > 0: n, k = n-1, k*n return k 8

  3. Tail Recursion From the Revised 7 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." Time Space Θ ( n ) Θ (1) def factorial(n, k): while n > 0: n, k = n-1, k*n return k 8

  4. Tail Recursion From the Revised 7 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)))) Time Space Θ ( n ) Θ (1) def factorial(n, k): while n > 0: n, k = n-1, k*n return k 8

  5. Tail Recursion From the Revised 7 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)))) Should use resources like Time Space Θ ( n ) Θ (1) def factorial(n, k): while n > 0: n, k = n-1, k*n return k 8

  6. Tail Recursion From the Revised 7 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 How? Eliminate the middleman! (factorial (- n 1) (* k n)))) Should use resources like Time Space Θ ( n ) Θ (1) def factorial(n, k): while n > 0: n, k = n-1, k*n return k 8

  7. Tail Recursion From the Revised 7 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 How? Eliminate the middleman! (factorial (- n 1) (* k n)))) Should use resources like Time Space Θ ( n ) Θ (1) def factorial(n, k): while n > 0: n, k = n-1, k*n return k (Demo) 8 http://goo.gl/tu9sJW

  8. Tail Calls

  9. Tail Calls 10

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

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

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

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

  14. 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 (define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) ) 10

  15. 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 (define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) ) 10

  16. 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 (define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) ) 10

  17. 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 (define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) ) 10

  18. 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 (define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) ) 10

  19. 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 (define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) ) 10

  20. 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 (define (factorial n k) (if (= n 0) k (factorial (- n 1) (* k n)) ) ) 10

  21. Example: Length of a List 11

  22. Example: Length of a List A call expression is not a tail call if more computation is still required in the calling procedure. 11

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

  24. Example: Length of a List (define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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

  25. Example: Length of a List (define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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

  26. Example: Length of a List (define (length s) (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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

  27. Example: Length of a List (define (length s) Not a tail context (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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

  28. Example: Length of a List (define (length s) Not a tail context (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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. (define (length-tail s) 11

  29. Example: Length of a List (define (length s) Not a tail context (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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. (define (length-tail s) (define (length-iter s n) 11

  30. Example: Length of a List (define (length s) Not a tail context (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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. (define (length-tail s) (define (length-iter s n) (if (null? s) n 11

  31. Example: Length of a List (define (length s) Not a tail context (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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. (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) 11

  32. Example: Length of a List (define (length s) Not a tail context (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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. (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) (length-iter s 0) ) 11

  33. Example: Length of a List (define (length s) Not a tail context (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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. (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) (length-iter s 0) ) 11

  34. Example: Length of a List (define (length s) Not a tail context (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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. (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) (length-iter s 0) ) 11

  35. Example: Length of a List (define (length s) Not a tail context (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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. (define (length-tail s) (define (length-iter s n) (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) (length-iter s 0) ) 11

  36. Example: Length of a List (define (length s) Not a tail context (if (null? s) 0 (+ 1 (length (cdr s)) ) ) ) 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. (define (length-tail s) (define (length-iter s n) Recursive call is a tail call (if (null? s) n (length-iter (cdr s) (+ 1 n)) ) ) (length-iter s 0) ) 11

  37. Eval with Tail Call Optimization 12

  38. Eval with Tail Call Optimization The return value of the tail call is the return value of the current procedure call. 12

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

  40. 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. (Demo) 12

  41. Tail Recursion Examples

  42. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  43. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  44. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  45. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  46. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  47. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  48. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  49. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  50. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  51. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  52. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  53. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  54. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  55. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  56. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  57. Which Procedures are Tail Recursive? Θ (1) Which of the following procedures run in constant space? ;; Compute the length of s. ;; Return whether s contains v. ( define (length s) ( define (contains s v) (+ 1 ( if (null? s) ( if (null? s) -1 false (length (cdr s))) ) ) ( if (= v (car s)) true ;; Return the nth Fibonacci number. (contains (cdr s) v)))) ( define (fib n) ( define (fib-iter current k) ;; Return whether s has any repeated elements. ( if (= k n) ( define (has-repeat s) current ( if (null? s) (fib-iter (+ current false (fib (- k 1))) ( if (contains? (cdr s) (car s)) (+ k 1)) ) ) true ( if (= 1 n) 0 (fib-iter 1 2))) (has-repeat (cdr s))) ) ) 14

  58. Map and Reduce

  59. Example: Reduce 16

  60. Example: Reduce (define (reduce procedure s start) 16

  61. Example: Reduce (define (reduce procedure s start) (reduce * '(3 4 5) 2) 16

  62. Example: Reduce (define (reduce procedure s start) (reduce * '(3 4 5) 2) 120 16

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

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

Recommend


More recommend