1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #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")))))
4 12687dd9 2023-08-04 jrmu ;Data Definition
5 12687dd9 2023-08-04 jrmu ;A list-of-digits is either
6 12687dd9 2023-08-04 jrmu ;1. an empty list or
7 12687dd9 2023-08-04 jrmu ;2. (cons n lod) where n is a number and lod is a list-of-digits.
9 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
10 12687dd9 2023-08-04 jrmu ;convert : list-of-digits -> number
11 12687dd9 2023-08-04 jrmu ;Converts a-list to a number where the first digit in a-list is
12 12687dd9 2023-08-04 jrmu ;the least significant digit in the number output, the second
13 12687dd9 2023-08-04 jrmu ;digit the second least, and so forth.
16 12687dd9 2023-08-04 jrmu ;(define (convert a-list)
18 12687dd9 2023-08-04 jrmu ; [(empty?) ...]
22 12687dd9 2023-08-04 jrmu ; (number? (first a-list))
24 12687dd9 2023-08-04 jrmu ; ... (first a-list) (convert (rest a-list)) ...]
25 12687dd9 2023-08-04 jrmu ; [(number? (first a-list)) ...]
26 12687dd9 2023-08-04 jrmu ; [else (error '... "...")]
27 12687dd9 2023-08-04 jrmu ; [else ...])]))
29 12687dd9 2023-08-04 jrmu ;;State Variables
30 12687dd9 2023-08-04 jrmu ;;guess# : N
31 12687dd9 2023-08-04 jrmu ;;Indicates the number of guesses taken.
33 12687dd9 2023-08-04 jrmu (define guess# 0)
35 12687dd9 2023-08-04 jrmu (define (convert a-list)
37 12687dd9 2023-08-04 jrmu [(empty? a-list) 0]
38 12687dd9 2023-08-04 jrmu [(cons? a-list)
41 12687dd9 2023-08-04 jrmu (number? (first a-list))
42 12687dd9 2023-08-04 jrmu (>= (first a-list) 0)
43 12687dd9 2023-08-04 jrmu (<= (first a-list) 9))
44 12687dd9 2023-08-04 jrmu (+ (first a-list) (* 10 (convert (rest a-list))))]
45 12687dd9 2023-08-04 jrmu [else (error 'convert "expected arg: list (of digits)")])]
46 12687dd9 2023-08-04 jrmu [else (error 'convert "expected arg: list (of digits)")]))
48 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
49 12687dd9 2023-08-04 jrmu ;check-guess-for-list : list-of-digits number -> symbol
50 12687dd9 2023-08-04 jrmu ;Consumes a-list, which is a list-of-digits and converts it
51 12687dd9 2023-08-04 jrmu ;to a number according to the function convert. It compares this
52 12687dd9 2023-08-04 jrmu ;to chosen, which is a number and returns the corresponding symbol
53 12687dd9 2023-08-04 jrmu ;'TooSmall, 'Perfect, or 'TooLarge
57 12687dd9 2023-08-04 jrmu (define (check-guess-for-list a-list chosen)
58 12687dd9 2023-08-04 jrmu (local ((define (check-guess-for-list-aux a-list chosen)
60 12687dd9 2023-08-04 jrmu [(= (convert a-list) chosen) 'Perfect]
61 12687dd9 2023-08-04 jrmu [(> (convert a-list) chosen) 'TooLarge]
62 12687dd9 2023-08-04 jrmu [(< (convert a-list) chosen) 'TooSmall])))
63 12687dd9 2023-08-04 jrmu (begin (set! guess# (add1 guess#))
64 12687dd9 2023-08-04 jrmu (check-guess-for-list-aux a-list chosen))))
66 12687dd9 2023-08-04 jrmu (guess-with-gui-list 1 check-guess-for-list)