;; Answer = (union 'correct 'lower 'higher) (define answer=? symbol=?) (define HI 100) (define LO 0) ;; Number -> Answer ;; returns a function that consumes a number -- g -- and ;; returns whether n is g, n if lower than g, or n is ;; higher than g. This of this function as someone who ;; is keeping a secret number (define (oracle n) (lambda (g) (cond [(= (abs (- g n)) 1) 'correct] [(< n g) 'lower] [(> n g) 'higher]))) ;; Example: A person with the secret number 10 (define person-with-10 (oracle 10)) ;; Ask the oracle about certain number compared to 10 ;; is the number 17 (answer=? (person-with-10 17) 'higher) ;; nope 17 is higher than secret number (answer=? (person-with-10 9) 'lower) ;; nope 9 is lower than the secret number (answer=? (person-with-10 10) 'correct) ;; nope 9 is lower than than the secret number ;; guess: (Number -> Answer) Number Number -> ;; returns true if we guess the correct number according to ;; an oracle p, starting with number n (define (guess p n) (guess* p n -1)) (define (guess* p n last) (cond [(symbol=? (p n) 'higher) (guess* p (round (/ (+ (if (or (= last -1) (< last n)) HI last) n) 2)) n)] [(symbol=? (p n) 'lower) (guess* p (round (/ (+ n (if (or (= last -1 ) (> last n)) LO last)) 2)) n)] [else true])) (guess (oracle 17) 97)