;; CS101, Spring 2013 ;; Lecture #13 ;; ; The READ function: ; ; read is a zero-parameter function that displays a box for entry in ; the interactions window and returns whatever is typed (up to, but ; not including the first space) as a quoted symbol, a number, or a ; string (a string to be read must be typed in quotations and can ; include the space character). ; ;(read) ; ; read is used to get input from the user of the program and should ; always be used after a printf that prompts the user for the data ; to be read. ; ; For example, the function get-name-and-age below prompts the user for ; their full name and age and then prints the information in the ; interactions window. ; ; General pattern for reading input: begin->printf->local->read ; ; Contract: (get-name-and-age) -> void; side-effect printing ; Header: (define get-name-and-age (lambda () ...)) ; Purpose: Prompt for and read a person's name and age and echo back the ; information in a printf statement. ; Pre-function tests: Not possible due to void return type ; Function definition: (define get-name-and-age (lambda () ;; need a begin below because body of function contains both ;; a printf and a local statement (begin (printf "Please enter your full name in quotation marks:~%") (local [(define name (read))] ;; need a begin below because a printf will precede the next local ;; statement (begin (printf "~%~%Please enter your age:~%") (local [(define age (read))] ;; print the result of the two read statements (printf "~%~%Name: ~a, Age: ~a~%~%" name age))))))) ; ; There is a slightly shorter form of the local expression that allows ; the definition of constants within a function. This form is called ; LET and is used in a similar way to local. ; ; General pattern for reading input: begin->printf->let->read ; ; Contract: (get-name-and-age-let) -> void; side-effect printing ; Header: (define get-name-and-age-let (lambda () ...)) ; Purpose: Prompt for and read a person's name and age and echo back the ; information in a printf statement. ; Pre-function tests: Not possible due to void return type ; Function definition: (define get-name-and-age-let (lambda () ;; need a begin here because body of function is a printf and a let ;; statement (begin (printf "Please enter your full name in quotation marks:~%") (let [(name (read))] ;; need a begin here because a printf will precede the next local ;; statement (begin (printf "~%~%Please enter your age:~%") (let [(age (read))] ;; print the result of the two read statements (printf "~%~%Name: ~a, Age: ~a~%~%" name age))))))) ; ; The pattern of combining a printf expression before a read statement ; as shown in the two functions above is known as the "prompt and read" ; pattern. ; ; The printf tells the user what entry is expected and the read statement ; inside a local or let saves the data for use in the body of the function. ; ; Advanced Student Language does not allow a define statement to be used ; inside a function without being preceded by a local special form. This ; means that a new local or let statement must be used to store the result ; of every separate read function that is done in response to a single ; prompt. ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; CLASS EXERCISE 1: ;; ;; Write a function to play Guess The Number with the computer. The game ;; goes as follows: The computer generates a random number between 1 and ;; 100 and then asks the user to guess the number. The program should ;; allow the user to continue guessing numbers until they guess the correct ;; number. If the guess is too low, the computer prints "Higher", and if ;; the guess is too high, the computer prints "Lower". The algorithm is ;; given below. ; Contract: (guess-the-number) -> void ; side-effect printing ; Header: (define guess-the-number (lambda () ... )) ; Purpose: Guess the number generated at random by the computer. ; Pre-function tests: None possible due to only side effect printing ; Function definition: ;(define guess-the-number ; (lambda () ; 1. Generate and save the random number. ; 2. Give user instructions: ; "Type a number between 1 and 100 and I'll give you hints" ; "to let you know if the correct answer is higher or lower." ; 3. Define a local function to loop until the correct answer is guessed. ; 4. Prompt for the user's guess: ; "Make your guess now:" ; 5. Read the guess. ; 6. if guess equals the correct answer, give them feedback: ; "YOU GOT IT!! The number is " ; 7. if guess is less than correct answer, tell user to guess higher ; and call local function recursively. ; 8. if guess is greater than correct answer, tell user to guess lower ; and call local function recursively. ; 9. Call local function defined in step 3. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; CLASS EXERCISE 2: ;; ;; Change the guess-the-number function so that it counts the number of ;; guesses the user makes and stops them at guess 5. Call the function ;; guess-the-number-5-tries. ; Contract: (guess-the-number-5-tries) -> void ; side-effect printing ; Header: (define guess-the-number-5-tries (lambda () ... )) ; Purpose: Guess the number generated at random by the computer. Only ; LIMIT number of guesses are allowed. ; Pre-function tests: None possible due to only side effect printing ; Function definition: ;(define guess-the-number-5-tries ; (lambda ()