How can i apply count function to render spec

Hello i was having difficulty using the function which i created which is supposed to display the number of guesses the program takes to find the players number.i have defined this function not sure if its correct ,

(define( number-of-guesses w)

   (add1 (number-of-guesses (guess w))))

i was trying to apply this to the render clause however

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

i get this error (start 1 100)
. . interval-small: contract violation
expected: interval?
given: 50

how and where should i place the function on my code

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

(define WIDTH 600)
(define HEIGHT 600)
(define TEXT-SIZE 10)
(define TEXT-X 20)
(define TEXT-UPPER-Y 50)
(define TEXT-LOWER-Y 50)
(define SIZE 36)

(define X-CENTER (quotient WIDTH 2))
(define Y-CENTER (quotient HEIGHT 2))

(struct interval (small big))

(define HELP-TEXT (text " UP for larger numbers, DOWN for smaller ones"
TEXT-SIZE
"Blue"))
(define HELP-TEXT2
(text "press = when your number is guessed; q to quit it."
TEXT-SIZE
"blue"))
(define COLOR "red")

(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 (start lower upper)
(big-bang (interval lower upper)
(on-key deal-with-guess)
(to-draw render )
(stop-when single? render-last-scene)))

(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 (smaller w)
(interval (interval-small w)
(max (interval-small w) (sub1 (guess w)))))
(define (bigger w)
(interval (min (interval-big w) (add1 (guess w)))
(interval-big w)))

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

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

(define (render-last-scene w)
(overlay (text "END" SIZE COLOR) MT-SC))

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

(define( number-of-guesses w)

   (add1 (number-of-guesses (guess w))))

So, first of all, a few thoughts on asking questions in general:

  1. You're showing us the error message and the code, which is great. Thank you.
  2. It helps if all the code is formatted, not just some of it.
  3. It helps if you can narrow the code down to only what's necessary. Simply going through the exercise of doing that will often let you discover the answer for yourself.
  4. It helps if you tell us what you've already tried. Did you google for the error message? Were there particular docs you tried, or lines of inquiry that you explored and didn't pan out? This can be summarized in a couple of brief sentences, such as "I googled and checked the docs but didn't find anything. I also tried (stepping through it in the debugger / adding print statements everywhere) but got nowhere."

As to your specific problem, the error message you're getting is this:

interval-small: contract violation
expected: interval?
given: 50

Here's what we can deduce from the message:

  1. It's a contract violation, which means that it's coming from a function call of some kind.
  2. It involves something of type 'interval', since the predicate that's being used is interval?
  3. Whatever is throwing the exception is being given a number and not whatever this interval thing is.
  4. An 'interval' in math means a range between two numbers, so this interval thing is probably a data structure of some kind that stores two numbers.
  5. It's a reasonable guess that 'interval-small' is supposed to be either a small interval or the low end of any interval.

Where in your code is there something called 'interval'? What kind of thing is it?

Once you've answered those questions you can figure out what specifically is causing the problem. You can do that either backwards or forwards:

Forwards: Where does this 'interval' thing get created and what path does it take through your code? How far does it get before it blows up?

Backwards: What line of code is generating the exception? What does that line do? Presumably it's operating on some piece of data, so where did that data come from?

Finally, a programming trick that will solve 95% of problems for you: Put logging everywhere and use that to see how your program executes. For example:

#lang racket

(define (make-list-of-numbers num)
  (displayln (format "entering make-list-of-numbers with num ~v" num))
  (begin0 ; run all statements inside the begin0, return the value of the *first*, not last, statement        
      (for/list ([i 10])
        (displayln (format "in the loop, i is ~a" i))
        (+ num i))
    (displayln "leaving make-list-of-numbers")))

(make-list-of-numbers 7)
(make-list-of-numbers 'a)

The output from this is:

entering make-list-of-numbers with num 7
in the loop, i is 0
in the loop, i is 1
in the loop, i is 2
in the loop, i is 3
in the loop, i is 4
in the loop, i is 5
in the loop, i is 6
in the loop, i is 7
in the loop, i is 8
in the loop, i is 9
leaving make-list-of-numbers
'(7 8 9 10 11 12 13 14 15 16)
entering make-list-of-numbers with num 'a
in the loop, i is 0
; +: contract violation                                                                                       
;   expected: number?                                                                                         
;   given: 'a                                                                                                                                 
> 

Based on that output, what was the error and what caused it?

displayln and println are a good way to get started with this, but you'll likely need to comment them out before running the program for real. Eventually you'll want to move on to proper log statements, but that's more complicated than you need right now.

2 Likes