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-beginner-reader.ss" "lang")((modname |37.4|) (read-case-sensitive #t) (teachpacks ((lib "convert.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp") (lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "convert.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp") (lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp") (lib "hangman.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 (define (convert a-list)
30 (cond
31 [(empty? a-list) 0]
32 [(cons? a-list)
33 (cond
34 [(and
35 (number? (first a-list))
36 (>= (first a-list) 0)
37 (<= (first a-list) 9))
38 (+ (first a-list) (* 10 (convert (rest a-list))))]
39 [else (error 'convert "expected arg: list (of digits)")])]
40 [else (error 'convert "expected arg: list (of digits)")]))
42 ;Contract, Purpose, Header
43 ;check-guess-for-list : list-of-digits number -> symbol
44 ;Consumes a-list, which is a list-of-digits and converts it
45 ;to a number according to the function convert. It compares this
46 ;to chosen, which is a number and returns the corresponding symbol
47 ;'TooSmall, 'Perfect, or 'TooLarge
48 ;
49 ;Template
51 (define (check-guess-for-list a-list chosen)
52 (cond
53 [(= (convert a-list) chosen) 'Perfect]
54 [(> (convert a-list) chosen) 'TooLarge]
55 [(< (convert a-list) chosen) 'TooSmall]))