CoreDump: A Guessing Game.

Anil Kumar B
3 min readApr 15, 2022

--

Welcome to CoreDump, the one-stop for techies.

Dump 04.14.2022.18.59

Monitor Tabby, discovers the world of monolithic Adhoc programmers, and Gecko markets modularity, design thinking, and various paradigms of design and testing.

Gecko:

I was marketing design thinking, lambdas, and hof, essentially reactive approaches, to a mentee at free willy BC, the mentee wanted to create a reactive design which is a guessing game! There is really no guessing with the engineering…

Lisa: I guess with ad hoc approaches, you keep guessing than nailing down the problem, the design constraints, and working out the trade-offs.

You miss out on complexity theory and maintainability and documentation.

I guess a story is essential in every codebase, the user stories remain for everybody.

Gecko: Documentation is automated, in any case, LISP and Racket are list-centered, so instead of streams you have lists and there is a world state list, an enumeration in a range. And there is data, the observer's set of numbers and there is one central function to the game, the guess() function.

The game is to count the number of iterations of the function guess, using HOF design on the list world state.

So we create lambdas for this to create monotonicity on the list, the list is sorted and we have an interval and use guess to find if the observer's data is in the bounded interval.

the function guess(),

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

here is a dump of the whole code for reference.

(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 count))

(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 count)
(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 (count w)
(interval (interval-count w)))


(define (smaller w)

(interval (interval-small w)

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

(add1 (interval-count w))))


(define (bigger w)

(interval (min (interval-big w) (add1 (guess w)))

(add1 (interval-count 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))


(define (render-last-scene w)
(overlay (text (number->string (count w)) SIZE COLOR) MT-SC))


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

The mentee was stuck:

“I am stuck making a helper function which should create the initial world state and should output an instance of an “interval” and to make that instance’s count be 0.

Below is my helper function, but I am not sure how I can modify to make it output an instance of the interval and set the instance count to zero.”

(define (count w)
(interval (interval-count w)

So all we needed was a map, to a count of the use of the guess() function on a range of values.

let us say that world view was a list of ordered numbers from lower to upper.

(in-range LOWER UPPER [1]) → stream?

(stream-map f s) → stream?

(define f ( lambda x (a < x <b))) -> stream?

defines an interval [a,b] and can output the stream , in-range a b [1]

--

--