;; code for pset 2 guessing games ;; setting up the hidden number ;; ;; gloabl variables to define range of possible values (define low-end 1) (define high-end 1000) ;; procedure for selecting a number within a given range at random (define guess (lambda (low high) ;; takes inputs defining range in which number could lie ;; and generates an integer that lies within that range by ;; using the Scheme primitive random (+ low (random (+ 1 (- high low)))))) ;; we can initially set our global variable, hidden, which is the value ;; we are trying to guess, by using the procedure to pick at random (define hidden (guess low-end high-end)) ;; to pick a new hidden number, we provide a procedure of no arguments ;; called make-hidden. You do not need to know the form of this procedure, ;; all you need to know is that calling (make-hidden) will cause the global ;; varaible hidden to take on a new value. (define how-many-trials (lambda (strategy strategy-low strategy-high low high) ;; input is the three procedures that constitute a strategy ;; plus the current values of the strategy's knowledge of ;; the low and high bounds on the value (let ((value (strategy low high))) ; get new guess by using strategy (cond ((= (test value) 0) ;; if it is correct 1) ;; then it took one try (else (+ 1 ;; otherwise count this try ;; and try again with a new low ;; and high bound on the range (how-many-trials strategy strategy-low strategy-high (strategy-low low high value) (strategy-high low high value)))))))) (define test (lambda (v) ;; this procedure is the only legal way to test a guess, ;; it returns 1 if the guess is too high, -1 if too low, ;; and 0 if correct (cond ((> v hidden) 1) ((= v hidden) 0) ((< v hidden) -1)))) (define square (lambda (x) (* x x))) (define measure-mean-and-variance (lambda (no-of-trials strategy strategy-low strategy-high) ;; procedures a strategy triple as input, together with a number ;; of trials to run. It iteratively counts the number of trials, the ;; running sum of the number of guesses over all trials and the running ;; sum of the squares of the number of guesses. It uses these to ;; compute the mean and deviation of the statistics for this strategy ;; over this number of trials (define helper (lambda (n sum sq-sum) ;; parameters are number of trials, ;; sum of trials, and sum of squares of trials (cond ((= n no-of-trials) ;; if we are done (let ((mean (/ sum n))) ;; create a temporary variable called ;; mean (see text for details on let) (newline) (display (round mean)) ;; print out mean number of guesses (newline) ;; print out the standard deviation of the guesses (display (round (sqrt (/ (+ sq-sum (* -2 mean sum) (* n (square mean))) (- n 1))))))) (else ;; if not done, then (make-hidden) ;; get a new hidden number ;; and run the strategy -- use let to ;; make a temporary variable to hold the result (let ((trial (how-many-trials strategy strategy-low strategy-high low-end high-end))) ;; iterate to the next trial, with new state variables (helper (+ n 1) (+ sum trial) (+ sq-sum (square trial)))))))) (helper 0 0 0)))