1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-advanced-reader.ss" "lang")((modname |37.4|) (read-case-sensitive #t) (teachpacks ((lib "guess-gui.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "guess-gui.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp")))))
5 ;A list-of-digits is either
7 ;2. (cons n lod) where n is a number and lod is a list-of-digits.
9 ;Contract, Purpose, Header
10 ;convert : list-of-digits -> number
11 ;Converts a-list to a number where the first digit in a-list is
12 ;the least significant digit in the number output, the second
13 ;digit the second least, and so forth.
16 ;(define (convert a-list)
22 ; (number? (first a-list))
24 ; ... (first a-list) (convert (rest a-list)) ...]
25 ; [(number? (first a-list)) ...]
26 ; [else (error '... "...")]
31 ;;Indicates the number of guesses taken.
35 (define (convert a-list)
41 (number? (first a-list))
43 (<= (first a-list) 9))
44 (+ (first a-list) (* 10 (convert (rest a-list))))]
45 [else (error 'convert "expected arg: list (of digits)")])]
46 [else (error 'convert "expected arg: list (of digits)")]))
48 ;Contract, Purpose, Header
49 ;check-guess-for-list : list-of-digits number -> symbol
50 ;Consumes a-list, which is a list-of-digits and converts it
51 ;to a number according to the function convert. It compares this
52 ;to chosen, which is a number and returns the corresponding symbol
53 ;'TooSmall, 'Perfect, or 'TooLarge
57 (define (check-guess-for-list a-list chosen)
58 (local ((define (check-guess-for-list-aux a-list chosen)
60 [(= (convert a-list) chosen) 'Perfect]
61 [(> (convert a-list) chosen) 'TooLarge]
62 [(< (convert a-list) chosen) 'TooSmall])))
63 (begin (set! guess# (add1 guess#))
64 (check-guess-for-list-aux a-list chosen))))
66 (guess-with-gui-list 1 check-guess-for-list)