CS 227 Lecture -*- Outline -*- Note: the point of this is the interactive program, like read-demo and interactive-square-root. Don't spend too much time on sqrt... * Input and Output (6.4) We'll write some programs that both read and write from the user. ** Example to introduce display and newline: Square Roots by Newton's Method. Show the Far side cartoon... Better reason to study square roots is it's an example of numerical methods. We'll use I/O to see what happens; a primitive kind of "visualization". ----------------- (square-root 100) ==> 10 (square-root 2) ==> 1.41421 ----------------- 5 decimal places accuracy. *** Idea ------------- Algorithm Idea: 1. Guess the answer 2. Keep improving the guess until it is good enough. ----------------- Something like "20 questions" game *** details How to improve the guess? ----------------- NEWTON'S METHOD To find sqrt(a) 1. Guess u = 1. 2. Let v = 1/2 (u + a/u). 3. If close enough, return v, else repeat. ----------------- the following should be omitted unless there is a question... Call the guess u, Call the number a. Want a function f, such that, | a - (f(a,u))^2 | < | a - u^2 | and thus f(a, sqrt(a)) = sqrt(a). this last equation says that sqrt(a) is a fixed point of f Newton's Method: Note 1/2 (sqrt(a) + a/sqrt(a)) = sqrt(a), so has a fix-point property. Because a/sqrt(a) = sqrt(a). If u is too big, a/u is too small, so average is better. If u is too small, a/u is too big, so average is again better. ----------------- ;; adapted from Program 6.2 (define square-root ; TYPE: (-> (number) number) (lambda (a) (letrec ((improve ; TYPE: (-> (number) number) (lambda (u) (/ (+ u (/ a u)) 2))) (next-estimate ; TYPE: (-> (number) number) (lambda (u) ; ENSURES: result*result is ; close enough to a (let ((v (improve u))) (if (close-enough? u v) v (next-estimate v)))))) (next-estimate 1.0)))) (define close-enough? ; TYPE: (-> (number number) boolean) (lambda (u v) (< (abs (- u v)) *tolerance*))) (define *tolerance* 0.000005) ----------------- Use the program and compare the results with built-in sqrt. *** How many decimal places accuracy do we get? (more than 5) Would like to see it getting closer to the answer to see how fast it finds the correct value. Estimate how many steps, etc. **** Modify the program as we did with tracing. -- Edit a copy into square-root-display. Put in writeln (note: display is not well motivated yet) and begin. -- Show how this works. **** Want to print intermediate results on 1 line. But writeln always gives a new line. -- need display part and newline part separated. -- 2 new procedures: display and newline. ***** display one argument, has side-effect of printing human readable output on screen. play with this. Show how strings work with display, and how spacing works. (begin (display 3) (display 4) 72) (begin (display 3) (display " ") (display 4) 72) ***** newline Takes no arguments, has side-effect of printing a newline, moves cursor. play with this. **** More editing... replace writeln with display. show how to get spacing after printing each add (display " ") and a newline at the end. put (begin (newline) v) in exit case *** Summary What does display do? What does newline do? What are their types? -------------- Input/Output Primitives display: (-> (datum) void) newline: (-> () void) write: (-> (datum) void) read: (-> () datum) -------------- ** read and write *** write Like display, but prints strings with double quotes. show this *** read Can enter data to a running process. No arguments. *** read-demo. (use it) do (load "ch6.ss") -- point out the "prompt" and "echo" -- prompts often have no newline at end. -- no arguments --- a thunk. ----------------- ;; Program 6.6 (define read-demo ; TYPE: (-> () void) (lambda () ; EFFECT: prompts, reads input, and ; echos it until "done" is entered. (display "Enter data ") (display "(enter done when finished)") (display ": ") (let ((response (read))) (cond ((eq? response 'done) (display "Thank you. Good-bye.")) (else (display "You entered: ") (write response) (newline) (read-demo)))))) ----------------- Use "EFFECT" to describe side effects intead of ENSURES the type says no arguments, and no results What happens if leave out the let in read-demo, and substitute (read) where response is? try it! What happens if replace write and newline with writeln? (so poorly named) This is the basic format (paradigm) for interactive programs ** interactive square root Now can use this interactive plan to write a program that prompts for numbers, and prints square roots. ----------------- ;; Program 6.7 (define interactive-square-root ; TYPE: (-> () void) (lambda () ; EFFECT: prompts for and read numbers ; printing their square roots, ; until "done" is entered. (writeln "Enter the number whose square root" " you want," " or enter done to quit:") (let ((n (read))) (if (eqv? n 'done) (writeln "That's all, folks.") (begin (writeln "The square root of " n " is " (square-root n)) (newline) (interactive-square-root)))))) ----------------- Point out why the begin is needed. If time describe the poly-print program. At least mention it.