Intervals: Trivial questions from an absolute beginner

Hi there.

I bought the HTDP book over a year ago as a newbie with no background in programming, gave it a little try, but then had to stop for a while...

Now I'm back at it with plenty of motivation, and I'm over the 120th page by now... Most things went well until now, but I'm still having trouble with some rather basic stuff...

For instance, my program for exercise n° 54 doesn't work, and I don't understand the error messages... I don't even know why there no on-tick function in the big-bang part of the program... Seems to me that the fly function should go with on-tick... And when I add it on my own, despite it not being in the book, it still doesn't work... etc... :confused:

So is it OK to ask noob question about how to fix noob stuff on here, or is this forum more of a pro venue for experienced programmers?

Thank you in advance... :slightly_smiling_face:

4 Likes

What was the error message?

1 Like

Hi

The on-tick is a part of the big-bang form, and the argument (add1 below) is the function that is called on every tick of Big Bang


(define (create-UFO-scene height)
  (underlay/xy (rectangle 100 100 "solid" "white") 50 height UFO))
 
(define UFO
  (underlay/align "center"
                  "center"
                  (circle 10 "solid" "green")
                  (rectangle 40 4 "solid" "green")))
 
 
(big-bang 0
          (on-tick add1)
          (to-draw create-UFO-scene))

Does the example help?

(example from < 2.4 Worlds and the Universe: "universe.rkt">)

1 Like

I forgot to answer your second question!

YES! The Racket community welcomes all learners :racket: :grinning: :heart:

5 Likes

Boy, I hope so, because otherwise I'll feel bad about the mountains of incredibly noob questions that I asked when I was getting started!

5 Likes

I’ll piggyback onto Stephen’s useful example and explanation, adding just a bit that I’ve often had to clarify for my students:

The function you give in the on-tick part of big-bang — here, add1 -- is called on every tick of big-bang, calling the function specified with the current value of the world at that point.

(So, for:

(big-bang 0
(on-tick add1)
(to-draw create-UFO-scene))

…the first tick of big-bang causes (add1 0) to be called, changing the world’s value to 1.

Then, on the next tick of big-bang, (add1 1) will be called, changing the world’s value to 2.

…and so on!)

— Sharon Tuttle

4 Likes

Hi there!

Thank you guys for all the the responses... Let me try tro adress them all with @mentions in this post.

@soegaard : Well, there is a few check-expects that differ from the expected value, but mainly I'm concerned about why my cond expression tells me none of it's conditions are met, when I feed it the string "resting" as an argument to the main function, while it's first condition is [(string? x) x]... This first one should yield true right?...Going from there, I don't even know if I can make sense of the check-expects... :confused:

@spdegabrielle : Thank you for the welcome! :slight_smile:
I have already made use of the on-tick function as a means to add one to an argument. They've had names like tok or advance-kitty, but what I don't understand here, is why it's not even mentioned at the end of this section of the book... But then again, if it yields a string, as it is supposed to before user input, how would the number one be added to a string?... So I was wondering if I was supposed to come up with an alternative, but I don't see how as of yet...
Besides, the fact the cond expression doesn't accept "resting" as a string breaks all my assumptions, so while I was able to work through faulty check-expects until now, here I have no idea what to do, or if my tries haven't broken the program even more... :confused:
Should I post the program here?

@dstorrs : Haha! I feel better already, thanks! :smile:

@smtuttle : Hi! Yeah no, I get that... What I don't understand, is why it's not mentioned in the example, so that it can call the fly function incrementally... Maybe I should make a helper function to feed on-tick instead of fly, and this new one would yield only numbers?...

I'm sorry, I know I might very well not be savvy enough to even ask the right questions the right way, but I hope this post will at least help me see what is wrong in the way I understand these things... And I promise I'll try to make my questions better as I go along...

Thanks again to all of you! :slight_smile:

2 Likes

Why don't you post your cond expression here, that is, the function that contains cond and how you apply it/test it?

1 Like

Hey... This is embarrassing on many levels... The cond expression doesn't give me the same error... But I can't have imagined it, because I'm not knowledgeable enough, and it did do it several times... Besides, I'm realising that the I probably didn't get the previous hints right... The rocket going up means the height is getting smaller, and kept focusing on the on-tick function... I'll post my three function program here, but all of this tells me that my grasp is not strong enough as it is... I think I'll make a pause, and make a last attempt using the Edx video course, instead of the book... Might be more approachable for a guy with my limitations... Thank you all!

;; A big-bang program that displays a rocket, that launches after a countdownd
;; of three seconds when the user hits de spacebar.

(require 2htdp/image)
(require 2htdp/universe)

;; CONSTANTS:
;;============

(define HEIGHT 300)
(define WIDTH 100)
(define YDELTA 3)
(define BACKG (empty-scene WIDTH HEIGHT))
(define ROCKET (above (triangle 10 "solid" "blue") (rectangle 10 30 "solid" "red")))
(define CENTER (/ (image-height ROCKET) 2))

;;===

;; DATA DEFINITIONS:
;;===================

;; An LRCD (stands for: lauching rocket countdown) is one of:

;; -- "resting"
;; -- a Number between -3 and -1
;; -- a NonnegativeNumber

;; Interpretation: a grounded rocket, then in countdown mode, then a number denotes
;; the number of pixels between the top of the canvas and the rocket, its height. Smaller ;;number means ricket is higher.

;; FUNCTIONS:
;;============

;; LRCD -> LRCD
;; A main function to start the program from a chosen state. Ex: (main1 "resting")

(define (main1 s)
  (big-bang s
    [to-draw show]
    ;; [on-tick fly]
    [on-key launch]
    ))

;; LRCD -> IMAGE
;; Renders the state as a resting or flying rocket

(check-expect
 (show "resting")
 (place-image ROCKET 10 (- HEIGHT CENTER) BACKG))

(check-expect
 (show -2)
 (place-image (text "-2" 20 "red")
              10 (* 3/4 WIDTH)
              (place-image ROCKET 10 (- HEIGHT CENTER) BACKG)))

(check-expect
 (show 53)
 (place-image ROCKET 10 (- 53 CENTER) BACKG))

(check-expect
 (show HEIGHT)
 (place-image ROCKET 10 (- HEIGHT CENTER) BACKG))

(check-expect
 (show 0)
 (place-image ROCKET 10 (- 0 CENTER) BACKG))

(define (show x)
  (cond
    [(string? x) (place-image ROCKET 10 (- HEIGHT CENTER) BACKG)]
    [(<= -3 x -1) (place-image (text (number->string x) 20 "red")
                               10 (* 3/4 WIDTH)
                               (place-image ROCKET 10 HEIGHT BACKG))]
    [(>= x 0) (place-image ROCKET 10 (- HEIGHT CENTER) BACKG)]))

;; LRCD KeyEvent -> LRCD
;; Starts the countdown when space-bar is pressed, if the rocket is still resting.

(check-expect (launch "resting" " ") -3)
(check-expect (launch "resting" "a") "resting")
(check-expect (launch -3 " ") -3)
(check-expect (launch -1 " ") -1)
(check-expect (launch 33 " ") 33)
(check-expect (launch 33 "a") 33)

(define (launch x ke)
  (cond
    [(string? x) (if
                  (string=? " " ke)
                  -3
                  x)]
    [(<= -3 x -1) x]
    [(>= x 0) x]))

;; LRCD -> LRCD
;; Raises the rocket by YDELTA, if it is movieng already.

(check-expect (fly "resting") "resting")
(check-expect (fly -3) -2)
(check-expect (fly -2) -1)
(check-expect (fly -1) HEIGHT)
(check-expect (fly 10) (- 10 YDELTA))
(check-expect (fly 22) (- 22 YDELTA))

(define (fly x)
  (cond
    [(string? x) x]
    [(<= -3 x -1) (if
                   (= x -1)
                   HEIGHT
                   (+ x 1))]
    [(>= 0 x) (- x YDELTA)]))

;; Running the program:
;;======================

;; (main1 "resting")
1 Like

You did not imagine it at all. But you commented out (main “resting”)` and therefore the error never materialized.

Take a look at the cond expression in fly:

(define (fly x)
   (cond
     [(string? x) x]
     [(<= -3 x -1) (if (= x -1) HEIGHT (+ x 1))]
     [(>= 0 x) (- x YDELTA)]))

Compare it to the data definition:

;; An LRCD (stands for: lauching rocket countdown) is one of: 
;; -- "resting" 
;; -- a Number between -3 and -1 
;; -- a NonnegativeNumber

The first line of the DD corresponds to the (string? x) condition, the first one. Check.
The second line of the DD corresponds to (<= -3 x -1), the second condition. Check.
The third line of the DD corresponds to? Not (>= 0 x). Because this condition says “x myst be Negative or Zero.”

(This direct correspondence between the DD and the cond TEMPLATE is the key to programming.)

So your mistake was one of notation not comprehension.

The error report from DrRacket/BSL was subpar.

  1. The good news is, the IDE was totally correct in pointing out this cond as the problem.
  2. The bad news is, the error message did not inform you of the value of x so that you could perform the across cross-check.

How can you help yourself here?

Since the hiliting tells you it's about the fly function, apply it to values from your DD: "resting", -2, 222. See what happens. In this case, the third value causes an error again. Now you can conduct the cond v DD inspection above.

3 Likes

Hi @EmEf ... Thanks for the answer... And sorry for being this late with the response...
After I was frustated for not getting an exercice this simple right, I went and hid under a rock for a couple of days... But if I'm to get anywhere with Racket, I guess I have to "power through" the feeling of embarassement I get sometimes...

Thank you for pointing the error in notation to me, I'll try it with the condition corrected, and I'll report back as soon as I do! Thanks again! :slight_smile:

4 Likes

Don't give up. Just holler when you're stuck, but do post the code that causes problems. There are lots of helpful people on this forum.

3 Likes

Seconding what EmEf said: don't give up and don't be embarrassed to ask for help here. No one is going to make fun of you; we've all been there, done that, made the exact same sort of mistakes you're making now and can sympathize with the frustration you're feeling.

4 Likes