CSC151 2007S, Class 23: Recursion with Files Admin: * EC for tomorrow's Thursday extra (at noon) and convocation (at 11 a.m.). * EC G-Tones 7:30 Friday, Bucksbaum Rotunda * EC for Jason Drwal's Meditation Study - Contact him * Meditating itself gives you benefit! * Are there any questions on HW8 or HW9? Overview: * Remaining I/O Topic: Repetitive Prompting. * About Files. * Key File Operations. * File Recursion. * Lab. /Context/a (display "Please enter a value and I will square it: ") (define val (read)) (define val-squared (* val val)) (display (string-append "The value of " (number->string val) " squared is " (number->string val-squared))) (newline) You've cleaned it up. Exercise 5: Error Checking a. What happens in your square program if someone enters something other than a number? b. Update your program so that it prints a friendly error message (using display) and then asks again if someone enters something other than a number. (letrec ((kernel (lambda () (display "Please enter a number and I'll square it: ") (let ((num (read))) (if (number? num) (let ((result (* num num))) ; (display "I bet you didn't know that ") ; (display "Emily thinks that ") (display num) (display " squared is ") (display result) (display ".") (newline)) (begin (display "Sam thinks that you are a bit clueless today, given that ") (display "you claimed that ") (display num) (display " is a number.") (newline) (kernel))))))) (kernel)) /On to Files/ * Idea: Store values for future use * Programmers need to figure out how to manipulate these thingsa * Scheme provides mechanisms for doing so * But makes them a bit complex * You don't deal directly with files; rather, you deal with ports * We convert files to ports, which give a partial view of the file * Use input ports for input * Use output ports for output * Why ports? * Multiple input from the same file * Models other things, too, such as Internet connections * Conversion (open-input-file "filename") (open-output-file "filename") * Input operations (read INPUT-PORT) (read-char INPUT-PORT) * Output operations (display value OUTPUT-PORT) (write value OUTPUT-PORT) (newline OUTPUT-PORT) (read) reads the next Scheme value: * If the next thing in the file starts with a character, read a symbol * Else If the next thing in the file startws with a digit, read a number * Else If the next thing in the file is an open paren read a list * Else If the next thing in the file is " read a string ... Why close your input ports and output ports? * Because our system limits how many open ports you can have * And although it's about 1000, you can run out