Download presentation
Presentation is loading. Please wait.
1
CS 403: Programming Languages
Lecture 8 Fall 2003 Department of Computer Science University of Alabama Joel Jones
2
Announcement Graduate School Preview Day Thursday, September 25
Starts 3PM Ferguson Theater Breakout sessions 4:15–5 Engineering 307 Ferguson Reception 5–5:30 in North Ferguson Lobby More information on flyer Lecture 8 ©2003 Joel Jones
3
Outline Questions Answers to last time’s exercises
Induction and Recursion Reading for next time Lecture 8 ©2003 Joel Jones
4
Questions Lecture 8 ©2003 Joel Jones
5
Problems (sumsquares n) (list1 x) (list2 x y) (list3 x y z) (atom? x)
(equal l1 l2) – We’ll see this later Lecture 8 ©2003 Joel Jones
6
Why recursion and induction?
Another tool for thinking about problems Easier to reason about recursive programs (!) Certain data structures are naturally recursive Pair Up: • What data structures are naturally recursive? Lecture 8 ©2003 Joel Jones
7
Induction and Recursion
“When programming recursively, think inductively” Terms/Concepts Precondtion or assumption Postcondition or guarantee Base case Recursive or inductive case Lecture 8 ©2003 Joel Jones
8
Exponentiation Version 1
If n ≥ 0, then exp n evaluates to 2n Pair Up: • What is the precondition? • What is the postcondition? ; p. 227 (217) (define exp (lambda (n) (if (= n 0) 1 (* 2 (exp (- n 1)))))) Lecture 8 ©2003 Joel Jones
9
Exponentiation Version 1 (cont.)
If n ≥ 0, then exp n evaluates to 2n ; p. 227 (217) (define exp (lambda (n) (if (= n 0) 1 (* 2 (exp (- n 1)))))) Does this code satisfy the specification? Pair Up: • Give an inductive proof. (Reminder of inductive proofs: prove the base case, prove the inductive case) Lecture 8 ©2003 Joel Jones
10
Exponentiation Version 1 (cont.)
Running time as a function of n T(0) = O(1) T(n + 1) = O(1) + T(n) So T(n) = O(n) Lecture 8 ©2003 Joel Jones
11
Exponentiation Version 2
Can we do better? Yes! ; p. 229 (219) (define square (lambda (n) (* n n))) (define double (lambda (n) (+ n n))) (define fast_exp (lambda (n) (if (= n 0) 1 (if (= 0 (mod n 2)) (square (fast_exp (div n 2))) (double (fast_exp (- n 1))))))) Does this code satisfy the specification. Yes, and we can prove it using complete induction, a form of mathematical induction in Which we may prove that n > 0 has a desired property by assuming not only that the predecessor has it, but that all preceeding numbers have it, and arguing that therefore n must have it. Here’s how it is done. For n = 0, the argument is exactly as before. Suppose, then, that n > 0. If n is even, the value of exp n Is the result of squaring the value of exp (n / 2). Inductively this Value is 2^(n/2), so squaring it yields 2^(n/2) * 2^(n/2) = 2^(n/2) = 2^n, as required. If, on the hand, n is odd, the value is the result of doubling exp (n - 1). Inductively the latter value is 2^(n - 1), so Doubling it yields 2^n Lecture 8 ©2003 Joel Jones
12
Exponentiation Version 2 (cont.)
Given T(0) = O(1) T(2n) = O(1) + T(n) T(2n + 1) = O(1) + T(2n) = O(1) + T(n) So T(n) = O(lg n) Lecture 8 ©2003 Joel Jones
13
But who cares? The general pattern of base case and recursive case carries through to many scheme functions And to handling recursive data structures in other languages (define f (lambda (n) (if (basecase? n) (dobase n) (combine (current n) (f (reduce n))))) Lecture 8 ©2003 Joel Jones
14
Example of induction on lists
(equal l1 l2) Pair Up: • Use the pattern above (or a more complicated version) to write equal on two lists) Lecture 8 ©2003 Joel Jones
15
Reading & Questions for Next Class
Online version of the book “Introduction to ML Programming” chapters 25 and 26, at: I have placed corresponding Scheme code on web site Lecture 8 ©2003 Joel Jones
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.