;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname week8_m_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; a nat is an integer greater than or equal to 0. ; make-secret : nat -> (nat -> symbol) ; returns a secret g (define (make-secret n) (local [(define secret (random n))] (lambda (guess) (cond [(< guess secret) 'too-low] [(= guess secret) 'just-right] [(> guess secret) 'too-high])))) (define my-secret (make-secret 400)) ; guess-secret : (nat -> symbol) -> nat ; finds secret value for secret (define (guess-secret secret) (guess-helper-binary-search secret 0 399) ) ; guess-helper : (nat -> symbol) nat -> nat ; finds secret value for secret assuming secret is at least n (define (guess-helper secret n) (cond [(symbol=? 'just-right (secret n)) n] [else (guess-helper secret (+ n 1))])) ; guess-helper-binary-search : (nat -> symbol) nat nat -> nat ; finds secret value for secret using binary search ; assuming number is less than or equal to above ; and is greater than or equal to below (define (guess-helper-binary-search secret below above) (local [(define guess (quotient (+ below above) 2)) (define ans (secret guess))] (cond [(symbol=? ans 'just-right) guess] [(symbol=? ans 'too-low) (guess-helper-binary-search secret (+ 1 guess) above)] [(symbol=? ans 'too-high) (guess-helper-binary-search secret below (- guess 1))])))