;; 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 (the string must be typed in quotations). ;; ;(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 following function prompts the user ;; for their full name and age and then prints the information to the ;; interactions window. ; 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 here because body of function is a printf and a local ;; statement (begin (printf "Please enter your full name in quotation marks:~%") (local [(define name (read))] ;; need a begin here 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))))))) ;; This pattern of combining a printf expression before a read statement is ;; known as "prompt and read". The printf tells the user what entry is ;; expected and the read statement reads the data. The local and define ;; special forms are used to combine the printing and reading in a single ;; 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 statement must be used to store the ;; result of every read function. ;; There is a slightly shorter form of the local expression that allows ;; the definition of value holders within a function. This form is called ;; LET and is used in a similar way to local. ;; General pattern: 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))))))) ;; 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. ; 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 (define guess-the-number (lambda () (local ; 1. Generate and save the random number [(define CORRECT (add1 (random 100)))] ; 2. Give user instructions (begin (printf "Type a number between 1 and 100 and I'll give you hints~%") (printf "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 (local [(define keep-guessing (lambda () (begin ; 4. Prompt for the user's guess (printf "Make your guess now: ~%") (local ; 5. Read the guess. [(define GUESS (read))] ; 6. if guess equals the correct answer, give them feedback. (if (= GUESS CORRECT) (printf "YOU GOT IT!! The number is ~a.~%" CORRECT) ; 7. if guess is less than correct answer, tell user to ; guess higher (if (> CORRECT GUESS) (begin (printf "Higher~%") (keep-guessing)) ; 8. if guess is greater than correct answer, tell user to ; guess lower (begin (printf "Lower~%") (keep-guessing))))))))] ; 9. Call inner guessing function (keep-guessing)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 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 (define LIMIT 5) ; Function definition: (define guess-the-number-5-tries (lambda () (local ; 1. Generate and save the random number [(define CORRECT (add1 (random 100)))] ; 2. Give user instructions (begin (printf "Type a number between 1 and 100 and I'll give you hints~%") (printf "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 (local [(define keep-guessing (lambda (acc) (begin (if (> acc LIMIT) (begin (printf "You have already used all ~a guesses.~%" acc) (printf "The number was ~a~%" CORRECT)) (begin ; 4. Prompt for the user's guess (printf "Make guess ~a now: ~%" acc) (local ; 5. Read the guess. [(define GUESS (read))] ; 6. if guess equals the correct answer, give them feedback. (if (= GUESS CORRECT) (printf "YOU GOT IT IN ~A GUESSES!! The number is ~a.~%" (+ 1 acc) CORRECT) ; 7. if guess is less than correct answer, tell user to ; guess higher (if (> CORRECT GUESS) (begin (printf "That was guess ~a. Higher!~%" acc) (keep-guessing (add1 acc))) ; 8. if guess is greater than correct answer, tell user to ; guess lower (begin (printf "That was guess ~a. Lower!~%" acc) (keep-guessing (add1 acc)))))))))))] ; 9. Call inner guessing function (keep-guessing 1))))))