class 09 recursion practice how recursive programs work
play

Class 09: Recursion practice, how recursive programs work Recall the - PowerPoint PPT Presentation

Class 09: Recursion practice, how recursive programs work Recall the list-length procedure (define (list-length alon) (cond [(empty? alon) 0] [(cons? alon) (+ 1 (list-length (rest alon)))])) Why does it correctly compute the length of any


  1. Class 09: Recursion practice, how recursive programs work

  2. Recall the list-length procedure (define (list-length alon) (cond [(empty? alon) 0] [(cons? alon) (+ 1 (list-length (rest alon)))])) • Why does it correctly compute the length of any list? • Does it work for lists of length zero? • How many of those are there? • Does it work for lists of length 1? • Yes… • Provided that the recursive call computing the length of the rest of the list works • The rest of the list has length 0, so its length does get computed correctly • Same argument for length 2, 3, 4, …

  3. Recursion diagrams and correct procedures • Recursion diagrams prepare for this logical analysis to work! • They ensure that the answer for length-zero lists is correct • They ensure that if the answer for length- (k-1) lists is correct, then the answer for length- k lists will be as well!

  4. Does it work at a mechanical level? • Do the rules of evaluation lead to the results you expect? • Yes (no surprise!) • We’ll see this by computing the length of a length-one list, using the rules.

  5. Warmup: cleaning up Boolean expressions

  6. A recursion problem • contains17? takes as input an int list , and returns true if one of the items is the number 17, and false if the list does not contain a 17. • The type-signature is contains17?: (int list) - > bool • With your neighbor, draw recursion diagrams for three cases: • (cons 13 (cons 4 empty)) • (cons 17 empty) • (cons 1 (cons 17 empty))

  7. OI: (cons 13 (cons 4 empty)) RI: (cons 4 empty) RO: false OO: false

  8. OI: (cons 17 empty) RI: empty RO: false OO: true

  9. OI: (cons 1 (cons 17 empty)) RI: (cons 17 empty) RO: true OO: true

  10. OI: (cons 1 (cons 17 empty)) RI: (cons 17 empty) RO: true If RO is true, OO is true If RO is false, but (first OO) is 17, then OO is true Otherwise OO is false OO: true

  11. Code (define (contains17? aloi) (cond [(empty? aloi) false] [(cons? aloi) (if (contains17? (rest aloi)) true (if (= 17 (first aloi)) true false))]))

  12. Code (define (contains17? aloi) (cond [(empty? aloi) false] [(cons? aloi) ( or (contains17? (rest aloi)) (if (= 17 (first aloi)) true false))]))

  13. Code (define (contains17? aloi) (cond [(empty? aloi) false] [(cons? aloi) ( or (contains17? (rest aloi)) (if (= 17 (first aloi)) true false))]))

  14. Code (better) (define (contains17? aloi) (cond [(empty? aloi) false] [(cons? aloi) ( or (contains17? (rest aloi)) (= 17 (first aloi))]))

  15. Code (even better) (define (contains17? aloi) (cond [(empty? aloi) false] [(cons? aloi) ( or (= 17 (first aloi)) (contains17? (rest aloi))]))

  16. Evaluation practice (already done previously, but left here for reference!)

  17. Color coding • Values are in green • Environment is a sequence of purple boxes; TLE shorthand shows just + and – as builtin procs.

  18. (skip to slide 42)

  19. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: B: C: Value:

  20. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: C: Value:

  21. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: C: Value: -------------------------------- B: (- 3 4) D E F D: E: F: value:

  22. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: C: Value: -------------------------------- B: (- 3 4) D E F D: builtin- E: F: value:

  23. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: C: Value: -------------------------------- B: (- 3 4) D E F D: builtin- E: 3 F: 4 value:

  24. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: C: Value: -------------------------------- B: (- 3 4) D E F D: builtin- E: 3 F: 4 value: -1

  25. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: -1 C: Value: -------------------------------- B: (- 3 4) D E F D: builtin- E: 3 F: 4 value: -1

  26. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: -1 C: Value: -------------------------------- B: (- 3 4) | C: (+ 2 5) D E F | G H J D: builtin- | G: E: 3 | H: F: 4 | J: value: -1 | value:

  27. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: -1 C: Value: -------------------------------- B: (- 3 4) | C: (+ 2 5) D E F | G H J D: builtin- | G: builtin+ E: 3 | H: F: 4 | J: value: -1 | value:

  28. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: -1 C: Value: -------------------------------- B: (- 3 4) | C: (+ 2 5) D E F | G H J D: builtin- | G: builtin+ E: 3 | H: 2 F: 4 | J: value: -1 | value:

  29. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: -1 C: Value: -------------------------------- B: (- 3 4) | C: (+ 2 5) D E F | G H J D: builtin- | G: builtin+ E: 3 | H: 2 F: 4 | J: 5 value: -1 | value:

  30. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: -1 C: Value: -------------------------------- B: (- 3 4) | C: (+ 2 5) D E F | G H J D: builtin- | G: builtin+ E: 3 | H: 2 F: 4 | J: 5 value: -1 | value: 7

  31. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: -1 C: 7 Value: -------------------------------- B: (- 3 4) | C: (+ 2 5) D E F | G H J D: builtin- | G: builtin+ E: 3 | H: 2 F: 4 | J: 5 value: -1 | value: 7

  32. (+ (- 3 4) (+ 2 5)) + builtin+ A B C builtin- - A: builtin+ B: -1 C: 7 Value: 6 -------------------------------- B: (- 3 4) | C: (+ 2 5) D E F | G H J D: builtin- | G: builtin+ E: 3 | H: 2 F: 4 | J: 5 value: -1 | value: 7

  33. (define (f x) (+ x 1)) + builtin+ (+ (f 1) (f 3)) builtin- - f closure{(x, (+ A B C x 1)) A: builtin+ B: C: Value: -------------------------------- B: (f 1) | C: (f 3) D E | G H D: closure (x, (+ x 1))| G: E: 1 | H: | | |

  34. (define (f x) (+ x 1)) + builtin+ (+ (f 1) (f 3)) builtin- - A B C A: builtin+ B: C: x 1 Value: -------------------------------- B: (f 1) | C: (f 3) D E | G H D: closure (x, (+ x 1))| G: E: 1 | H: Bind x -> 1; evaluate | (+ x 1) | | | |

  35. (define (f x) (+ x 1)) + builtin+ (+ (f 1) (f 3)) builtin- - A B C A: builtin+ B: C: x 1 Value: -------------------------------- B: (f 1) | J: (+ x 1) D E | K L M D: closure (x, (+ x 1))| K: builtin+ E: 1 | L: 1 Bind x -> 1; evaluate | M: 1 J: (+ x 1) | Value: 2 Value: | |

  36. (define (f x) (+ x 1)) + builtin+ (+ (f 1) (f 3)) builtin- - A B C A: builtin+ B: C: x 1 Value: -------------------------------- B: (f 1) | J: (+ x 1) D E | K L M D: closure (x, (+ x 1))| K: builtin+ E: 1 | L: 1 Bind x -> 1; evaluate | M: 1 J: (+ x 1) | Value: 2 Value: 2 | |

  37. (define (f x) (+ x 1)) + builtin+ (+ (f 1) (f 3)) builtin- - A B C A: builtin+ B: C: Value: -------------------------------- B: (f 1) | C: (f 3) D E | G H D: closure (x, (+ x 1))| G: E: 1 | H: Bind x -> 1; evaluate | (+ x 1) | value: 2 | Drop the new binding |

  38. (define (f x) (+ x 1)) + builtin+ (+ (f 1) (f 3)) builtin- - A B C A: builtin+ B: 2 C: Value: -------------------------------- B: (f 1) | C: (f 3) D E | G H D: closure (x, (+ x 1))| G: closure (x, (+ x 1)) E: 1 | H: 3 Bind x -> 1; evaluate | (+ x 1) | value: 2 | Drop the new binding |

  39. (define (f x) (+ x 1)) + builtin+ (+ (f 1) (f 3)) builtin- - A B C A: builtin+ B: 2 C: x 3 Value: -------------------------------- B: (f 1) | C: (f 3) D E | G H D: closure (x, (+ x 1))| G: closure (x, (+ x 1)) E: 1 | H: 3 Bind x -> 1; evaluate | Bind x -> 3; evaluate (+ x 1) | (+ x 1) value: 2 | value: 4 Drop the new binding | Drop the new binding

  40. (define (f x) (+ x 1)) + builtin+ (+ (f 1) (f 3)) builtin- - A B C A: builtin+ B: 2 C: 4 Value: -------------------------------- B: (f 1) | C: (f 3) D E | G H D: closure (x, (+ x 1))| G: closure (x, (+ x 1)) E: 1 | H: 3 Bind x -> 1; evaluate | Bind x -> 3; evaluate (+ x 1) | (+ x 1) value: 2 | value: 4 Drop the new binding | Drop the new binding

Recommend


More recommend