;; 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 week7_w_binary_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "htdp"))))) ;; make-secret : number -> (number -> symbol) ;; makes up a secret and then returns ;; a function that lets you guess the secret (define (make-secret n) (local [(define secret (random n))] (lambda (guess) (cond [(< guess secret) 'toosmall] [(= guess secret) 'yes] [(> guess secret) 'toobig])))) ;;guess-helper-binary-search: (number -> symbol) number number ; guesses number using binary search, assuming number is less than or equal to "above" and 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 'yes) guess] [(symbol=? ans 'toosmall) (guess-helper-binary-search secret (+ guess 1) above)] [(symbol=? ans 'toobig) (guess-helper-binary-search secret below (- guess 1))]))) (define sec1 (make-secret 4)) (guess-helper-binary-search sec1 0 4) (define sec2 (make-secret 4)) (guess-helper-binary-search sec2 0 4) (define sec4 (make-secret 4)) (guess-helper-binary-search sec4 0 4)