CSC151, Class 37: Input and Output Overview: * Procedures to generate output * Procedures to read input * Lab Notes: * Class Friday in Saints' Rest. Two classmates-on-crutches may need a ride. Volunteers? * Extra credit for attending "Fleeting Fictions". * Read "Files in Scheme" * Next year, should I make the Gimp a key subject of the class? * Questions on exam 2? ---------------------------------------- (define sum-of-number-tree (lambda (ntree) (if (pair? ntree) (+ (sum-of-number-tree (car ntree)) (sum-of-number-tree (cdr ntree))) ntree))) ---------------------------------------- Scheme does not behave like normal programs (not even like normal command-line programs) Moderately old interactive model of computation 1. User starts program 2. Program prompts for input 3. User gives input 4. Program computes 5. Return to step 2 Modern interactive model of computation 1. User starts program 2. User decides whether to click on a menu, button etc. 3. Program reacts Scheme model 1. User enters a Scheme expression 2. Scheme evaluates it 3. Go back to step 1 We need to be able to prompt for input and then read the input the user gives. Prompting: * (display val) Puts val on the screen If val is a string: Doesn't show quotation marks If val is a character: Doesn't show #\ * (newline) Starts a new line * (write val) Puts val on the screen If val is a string: Shows quotation marks If val is a character: Shows the #\ * (write-char val) Puts val on the screen If val is a string: Complains that val is not a character Val must be a character: Doesn't show #\ Input: * (read) Gets and returns a value (from the user) * (read-char) Gets and returns a character (from the user) ; Read n characters and return them as a list. (define read-n-chars (lambda (n) (if (zero? n) null (cons (read-char) (read-n-chars (- n 1))))))