Blob


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")))))
4 ;Data Definition
5 ;A list-of-digits is either
6 ;1. an empty list or
7 ;2. (cons n lod) where n is a number and lod is a list-of-digits.
8 ;
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.
14 ;
15 ;Template
16 ;(define (convert a-list)
17 ; (cond
18 ; [(empty?) ...]
19 ; [(cons?)
20 ; (cond
21 ; [(and
22 ; (number? (first a-list))
23 ; ())
24 ; ... (first a-list) (convert (rest a-list)) ...]
25 ; [(number? (first a-list)) ...]
26 ; [else (error '... "...")]
27 ; [else ...])]))
29 ;;State Variables
30 ;;guess# : N
31 ;;Indicates the number of guesses taken.
33 (define guess# 0)
35 (define (convert a-list)
36 (cond
37 [(empty? a-list) 0]
38 [(cons? a-list)
39 (cond
40 [(and
41 (number? (first a-list))
42 (>= (first a-list) 0)
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
54 ;
55 ;Template
57 (define (check-guess-for-list a-list chosen)
58 (local ((define (check-guess-for-list-aux a-list chosen)
59 (cond
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)