CS 227 lecture -*- Outline -*- Context: We've seen recursion on lists. Of course, computers also work with numbers, and we'll see how to do numeric recursion, and mixed combinations of numbers and lists. Also call rational numbers fractions whenever possible, to distinguish from the stuff we implement in next section. * Operations on Numbers (3.2) this is more examples of recursion, and another (!) exposure to data abstraction ** subsets of numbers *** in Scheme (concrete, inductive) play with the following ---------------- (define numeric-tester ; TYPE: (-> (number) void) (lambda (n) ; EFFECT: print some facts about n (begin (writeln "(real? " n ") ==> " (real? n)) (writeln "(rational? " n ") ==> " (rational? n)) (writeln "(integer? " n ") ==> " (integer? n)) ))) ----------------- Use the above on 3, 3., 3.14, (/ 1 3) Interesting points (real? 3) ==> #t (rational? 3) ==> #t (integer? 3.) ==> #t ; yes, true! (rational? 3.14) ==> #t ; yes, true! (real? (/ 1 3)) ==> #t *** summary, in math Draw a Venn diagram for the following --------------------- HIERERCHY OF NUMBERS (VALUES) I. Complex numbers (3+4i) A. Real Numbers (e, square root of 2) 1. Fractions (1/2, 7/3) a. Integers (1) i. Positive (0, 1, ...) also called the naturals and nonnegative ii. Negative (-1, -2, ...) 2. Non-fractions (root of 2) a. Transcendentals (pi) ---------------------- There is some lack of uniformity in what people call the naturals. (some start with 1) Hence the book often says nonnegative. Note what scheme calls a real is really a fraction *** conversions between subsets ---------------- (define conversion-tester ; TYPE: (-> (number) void) (lambda (n) ; EFFECT: print some facts about n (begin (writeln "(floor " n ") ==> " (floor n)) (writeln "(ceiling " n ") ==> " (ceiling n)) (writeln "(round " n ") ==> " (round n)) (writeln "(truncate " n ") ==> " (truncate n)) ))) ----------------- Try this with 3.499, 3.50, 3.501, and their negatives ** exactness *** in scheme ---------------- (define exactness-tester ; TYPE: (-> (number) void) (lambda (n) ; EFFECT: print some facts about ; the exactness of n (begin (writeln "(exact? " n ") ==> " (exact? n)) (writeln "(inexact? " n ") ==> " (inexact? n)) ))) ----------------- Try this with 0.33, (/ 1 3), and (+ 0.0 (/ 1 3)) summary: how to convert from exact to inexact? does the other way make sense? *** summary ---------------------- NUMBER OBJECTS * cannot represent all irrational numbers e.g., pi can only be approximated * Computers do not have built-in representations for irrationals I. Inexact (approximate) also called floating point when printed have a decimal point A. Fractional 3.33333333, 7.2e-24 B. Integer 3., 7.e10 II. Exact A. Integer 3, 703 B. Fractional (rational) 4, 1/3, 703/11 ----------------------- Think of 0.3333 as representing all the reals in the half-open interval [0.33325, 0.33335) ** observing numbers Q: what operations would you want, besides =, for comparing numbers? have them take 1 minute to get as many as possible ----------- NUMERIC PREDICATES = : (-> (exact-num exact-num) boolean) < : (-> (number number) boolean) <= : (-> (number number) boolean) > : (-> (number number) boolean) >= : (-> (number number) boolean) eqv? : (-> (number number) boolean) zero? : (-> (number) boolean) ----------- (eqv? 0.33333333 (/ 1 3)) ==> #f (eqv? 0.33333333 (+ 0.0 (/ 1 3))) ==> #f Be careful comparing inexact numbers for equality (in any lang) Point out danger of using = and zero? with inexact numbers ** arithmetic operations (making new numbers from old) + - * / seen already we will also use add1 and sub1 in recursion: also have things you'd find on a scientific calculator cos, sin, acos, asin, tan, atan, expt, sqrt, ... and a few others abs, min, max, quotient, remainder, modulo see the book p. 76 for details write add1 and sub1 on the board !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ** examples of recursion *** on numbers ----------------- HARMONIC SUM (harmonic-sum 0) = 0 (harmonic-sum n) = 1 + 1/2 + ... + 1/n harmonic-sum : ------------- e.g. show sum up to 3 Have them answer the following in groups Q: What is the type of harmonic sum? Q: When to stop? Q: What is one step? Q: What is a smaller journey? One way to write it out. ONE STEP and the REST OF THE JOURNEY Let n = (add1 k), i.e., n > 0 (harmonic-sum n) = 0, definition of harmonic-sum> 1 + 1/2 + ... + 1/(n-1) + 1/n = 0 + 1/1 + 1/2 + ... + 1/(n-1) + 1/n = 1/n + 1/(n-1) +... + 1/2 + 1/1 + 0 = 1/n + (harmonic-sum (sub1 n)) e.g. show sum up to 3 (define harmonic-sum ; TYPE: natural -> fraction (lambda (num) (if (zero? num) 0 (+ (/ 1 num) (harmonic-sum (sub1 num)))))) ----------------- SOME PROBLEMS (list-of-zeros 0) ==> () (list-of-zeros 3) ==> (0 0 0) list-of-zeros: (-> (number) (list number)) (list-ref '(a bb ccc) 0) ==> a (list-ref '(a bb ccc) 1) ==> bb (list-ref '(a bb ccc) 2) ==> ccc (list-ref '(a bb ccc) 3) =signals=> Error: list-ref: Index ... list-ref: (-> ((list T) natural) T) ----------------- break them into groups to write these have to explain how to make an error. (error "list-ref: Index" 3 "is out of range for list" '()) *** pattern what is the pattern? How does it compare to list recursion cdr ~ sub1 null? ~ zero? '() ~ 0 also add1 ~ cons ------------------ RECURSION ON NATURAL NUMBERS Theorem: each natural number object is either 0 or (add1 N) where N is a natural number NATURALS 0 : natural add1 : (-> (natural) natural) sub1 : (-> (natural) natural) zero?: (-> (natural) boolean) for all n: natural, (zero? 0) = #t (zero? (add1 n)) = #f (sub1 (add1 n)) = n ------------------ compare to lists *** on lists, using numbers (omit if low on time) -------------- (length '()) ==> 0 (length '(1 2)) ==> 2 length : (-> ((list T)) natural) -------------- this is recursion on lists *** (if time) discuss error checking and efficiency in list-ref check length each time through vs. not at all note: all but first check is useless, leading to... compromise: check once and call helper program 3.7 attempts to optimize this, but what kinds of error messages does it give vs the one on p. 80? will correct this later Show different versions of list-ref (list-ref.slides) Ask them to compare