;; CMPU 101, Spring 2013 ;; Lab7 (display "\n CS101 Lab 7, Spring 2013") (display "\n PLEASE WRITE YOUR NAME HERE\n\n") ; ; For this lab, you will write a program that simulates the ; flip of 3 fair coins and that counts the number of times 3 coins ; are flipped before a result of ALL 3 heads. ; ; Then, for the more adventurous of you, you can write a function ; to generate any number of heads for a given n (see problem 4). ; ; ; Problem 1. Define a zero-parameter function called FLIP ; that produces either 0 or 1 at random. The contract, ; purpose, pre-function tests, and post-function tests of ; this function are given below. ; (display "Problem 1. flip a coin\n\n") ;;Contract: (flip) -> number ;;Header: (define flip (lambda () ... )) ;;Purpose: uses the random function to generate a number ;; that is either 0 or 1 ;;Pre-function tests: ; (check-within (flip) 0 1) ; (check-within (flip) 0 1) ; (check-within (flip) 0 1) ; (check-within (flip) 0 1) ;;Function definition: ;;Post-function printfs: ; (printf "(flip) => ~a~%" (flip)) ; (printf "(flip) => ~a~%" (flip)) ; (printf "(flip) => ~a~%" (flip)) (newline) (newline) ; ; Problem 2. Define a one-parameter function called ; RETURN-FLIP-RESULT that consumes a number, flip, that ; is 0 or 1 and that returns the string "head" if flip = 0, ; or "tail" if flip = 1. ; ; Assume that no other number besides 0 or 1 will be ; entered. ; (display "Problem 2. return flip result\n\n") ;; The following constants, HEADS and TAILS, are to be ;; used in place of 0 or 1 when testing the result of ;; each coin flip: (define HEADS 0) (define TAILS 1) ;;Contract: (return-flip-result number(0or1)) -> string ;;Header: (define return-flip-result (lambda (flip) ... )) ;;Purpose: returns the string "head" if flip=0, or "tail" if ;; flip=1 ;;Pre-function tests: ;;Function definition: ;;Post-function printfs: (newline) (newline) ; ; Problem 3. Define a 0-parameter function called ; FLIP-3-HEADS. ; ; Write a 0-parameter function called flip-3-heads that ; contains an inner recursive accumulator function to count ; the number of trials needed to flip 3 heads when flipping ; 3 fair coins. The output of this function should be as ; shown below (although the number of trials may vary): ; ; > (flip-3-heads) ; ; Trial 1) You got tail head head, please try again. ; ; Trial 2) You got tail head head, please try again. ; ; Trial 3) You got head tail tail, please try again. ; ; Trial 4) You got head head tail, please try again. ; ; Trial 5) You got head tail head, please try again. ; ; Trial 6) You got tail tail head, please try again. ; ; Trial 7) You got tail head head, please try again. ; ; Trial 8) You got head head head!! Game over! ; ; ; In the inner recursive helper function, you should first ; store the value of 3 separate coin flips in local variables. ; Then check if the flips resulted in 3 heads. If so, use the ; printf function with the string argument as shown: ; ; (printf "Trial ~a) You got ~a ~a ~a!! Game over!~%" ; acc ...) ; ; If the flips did not result in 3 heads, use a printf ; like this to print the trial number and the result of ; 3 flips: ; ; (printf "Trial ~a) You got ~a ~a ~a, please try again.~%" ; acc ...) ; ; and then call the flip-3-heads function recursively. ; (display "Problem 3. flip 3 heads\n\n") ; Contract: (flip-3-heads) -> side-effect printing ; Header: (define flip-3-heads (lambda () ...)) ; Purpose: Repeat until 3 heads are thrown. ; ; Pre-function tests: None because of side-effect printing and ; randomized function. ; Function definition: ; Post-function printfs: Test function in interactions window. (newline) (newline) ; ; Problem 4. (OPTIONAL) Define a 1-parameter function called ; FLIP-N-HEADS. ; ; Write a 1-parameter function called flip-n-heads that ; consumes a parameter n and that contains one inner ; recursive accumulator function (or a higher-order list ; generating function) to generate a list of n 0s and 1s. ; Your function can then count the number of trials needed ; to flip n heads with n fair coins. The output of the ; flip-n-heads function should be as shown below (although ; the number of trials may vary): ; ; > (flip-n-heads 4) ; ; Trial 1) You got tail head head tail, please try again. ; ; Trial 2) You got tail head head head, please try again. ; ; Trial 3) You got head tail tail tail, please try again. ; ; Trial 4) You got head head head tail, please try again. ; ; Trial 5) You got tail head tail head, please try again. ; ; Trial 6) You got tail tail tail tail, please try again. ; ; Trial 7) You got tail tail head head, please try again. ; ; Trial 8) You got head tail head head, please try again. ; ; Trial 9) You got head head head head!! Game over! ; ; Hints: ; ; Store the value of n separate coin flips in a local list ; variable. ; ; Then check if the flips in the list resulted in n heads. ; You can use the built-in andmap function to do this (look ; up andmap in the help desk) or you can write your own ; recursive function. If the flips resulted in n heads, use ; the printf function with the string argument as shown: ; ; (printf "Trial ~a: You got ~a!! Game over!~%" ; acc (map return-flip-result lon)) ; ; return-flip-result is defined in Problem 2. ; ; If the flips did not result in n heads, use a printf ; like this to print the trial number and the result of ; n flips: ; ; (printf "Trial ~a) You got ~a, please try again.~%" ; acc (map return-flip-result lon)) ; ; return-flip-result is defined in Problem 2. ; ; and then call the flip-n-heads function recursively. ; (display "Problem 4. flip n heads\n\n") ; Contract: (flip-n-heads pos-integer) -> side-effect printing ; Header: (define flip-n-heads (lambda (n) ...)) ; Purpose: Repeat until n heads are thrown. ; ; Pre-function tests: None because of side-effect printing and ; randomized function. ; Function definition: ; Post-function printfs: Test function in interactions window.