I am now attempting to do the following ch5 question in Realm of Racket:

Change the Guess My Number game so that it displays the number of guesses the 
program takes to find the players number.  Hint: you might need to change the 
data used to represent the data used to represent the world's state.

So what I did was to extend the interval struct from:

(struct interval (small big)

to

(struct interval (small big guesses)


Then I changed smaller (and bigger) like this:

(define (smaller w)
  (interval (interval-small w)
            (max (interval-small w) 
                 (sub1 (guess w)))))

to

(define (smaller w)
  (interval (interval-small w)
            (max (interval-small w) 
                 (sub1 (guess w))) ((+ interval-guesses 1) w)))



But with these changes, when I run the program I get:

+: contract violation
  expected: number?
  given: #<procedure:interval-guesses>
  argument position: 1st
  other arguments...:
   1

In the Racket debugger it is complaining at the line (+ interval-guesses 1) 

But surely, (+ interval-guesses 1) when evaluated would return a number???

Why am I getting this error?  and how would I fix it?


Here is my full code for the program:

#lang racket
(require 2htdp/universe 2htdp/image)

(struct interval (small big guesses) #:transparent)


(define TEXT-SIZE 10)

(define HEIGHT 150)
(define COLOR "red")
(define SIZE 72)
(define TEXT-X 3)
(define TEXT-UPPER-Y 10)
(define TEXT-LOWER-Y 135)

(define HELP-TEXT
  (text "up arrow for larger numbers, down arrow for smaller ones"
        TEXT-SIZE
        "blue"))

(define HELP-TEXT2
  (text "Press = when your number is guessed; q for quit."
        TEXT-SIZE
        "blue"))

(define WIDTH (+ (image-width HELP-TEXT2) 10))

(define MT-SC
  (place-image/align
   HELP-TEXT TEXT-X TEXT-UPPER-Y "left" "top"
   (place-image/align
    HELP-TEXT2 TEXT-X TEXT-LOWER-Y "left" "bottom"
    (empty-scene WIDTH HEIGHT))))

(define (guess w)
  (quotient (+ (interval-small w) (interval-big w)) 2))

(define (render w)
  (overlay (text (number->string (guess w)) SIZE COLOR) MT-SC))

(define (smaller w)
  (interval (interval-small w)
            (max (interval-small w) 
                 (sub1 (guess w))) ((+ interval-guesses 1) w)))

(define (bigger w)
  (interval (min (interval-big w) 
                 (add1 (guess w)))
            (interval-big w) ((+ interval-guesses 1) w)))

(define (deal-with-guess w key)
  (cond [(key=? key "up") (bigger w)]
        [(key=? key "down") (smaller w)]
        [(key=? key "q") (stop-with w)]
        [(key=? key "=") (stop-with w)]
        [else w]))

(define (single? w)
  (= (interval-small w) (interval-big w)))

(define (start lower upper)
  (big-bang (interval lower upper 0)
            (on-key deal-with-guess)
            (to-draw render)
            (stop-when single? render)))


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to