;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #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"))))) ;Data Definition ;A list-of-digits is either ;1. an empty list or ;2. (cons n lod) where n is a number and lod is a list-of-digits. ; ;Contract, Purpose, Header ;convert : list-of-digits -> number ;Converts a-list to a number where the first digit in a-list is ;the least significant digit in the number output, the second ;digit the second least, and so forth. ; ;Template ;(define (convert a-list) ; (cond ; [(empty?) ...] ; [(cons?) ; (cond ; [(and ; (number? (first a-list)) ; ()) ; ... (first a-list) (convert (rest a-list)) ...] ; [(number? (first a-list)) ...] ; [else (error '... "...")] ; [else ...])])) ;;State Variables ;;guess# : N ;;Indicates the number of guesses taken. (define guess# 0) (define (convert a-list) (cond [(empty? a-list) 0] [(cons? a-list) (cond [(and (number? (first a-list)) (>= (first a-list) 0) (<= (first a-list) 9)) (+ (first a-list) (* 10 (convert (rest a-list))))] [else (error 'convert "expected arg: list (of digits)")])] [else (error 'convert "expected arg: list (of digits)")])) ;Contract, Purpose, Header ;check-guess-for-list : list-of-digits number -> symbol ;Consumes a-list, which is a list-of-digits and converts it ;to a number according to the function convert. It compares this ;to chosen, which is a number and returns the corresponding symbol ;'TooSmall, 'Perfect, or 'TooLarge ; ;Template (define (check-guess-for-list a-list chosen) (local ((define (check-guess-for-list-aux a-list chosen) (cond [(= (convert a-list) chosen) 'Perfect] [(> (convert a-list) chosen) 'TooLarge] [(< (convert a-list) chosen) 'TooSmall]))) (begin (set! guess# (add1 guess#)) (check-guess-for-list-aux a-list chosen)))) (guess-with-gui-list 1 check-guess-for-list)