# Intervals: Trivial questions from an absolute beginner

**URL:** <https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099>\
**Category:** Questions & Answers\
**Created:** [June 27, 2022, 6:45am UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099 "2022-06-27T06:45:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![late\_bloomer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/late_bloomer/32/428_2.png) [@late\_bloomer](https://racket.discourse.group/u/late_bloomer)\
**Post date:** [June 27, 2022, 6:45am UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/1 "2022-06-27T06:45:14Z")

</div>

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... 😕

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... 🙂

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [June 27, 2022, 7:32am UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/2 "2022-06-27T07:32:12Z")

</div>

What was the error message?

---

<div class="post-metadata">

**Author:** ![spdegabrielle](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/spdegabrielle/32/95_2.png) [@spdegabrielle](https://racket.discourse.group/u/spdegabrielle)\
**Post date:** [June 27, 2022, 8:04am UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/3 "2022-06-27T08:04:31Z")

</div>

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

```scheme

(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&nbsp;Worlds and the Universe: "universe.rkt"](https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28part._world-example%29)\>)

---

<div class="post-metadata">

**Author:** ![spdegabrielle](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/spdegabrielle/32/95_2.png) [@spdegabrielle](https://racket.discourse.group/u/spdegabrielle)\
**Post date:** [June 27, 2022, 9:18am UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/4 "2022-06-27T09:18:15Z")

</div>

> [@late\_bloomer](#):
>
> 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?

I forgot to answer your second question!

YES! The Racket community **welcomes** all learners :racket: 😀 ❤

---

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [June 27, 2022, 3:08pm UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/5 "2022-06-27T15:08:36Z")

</div>

> [@late\_bloomer](#):
>
> 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?

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!

---

<div class="post-metadata">

**Author:** ![smtuttle](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@smtuttle](https://racket.discourse.group/u/smtuttle)\
**Post date:** [June 28, 2022, 1:09am UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/6 "2022-06-28T01:09:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![late\_bloomer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/late_bloomer/32/428_2.png) [@late\_bloomer](https://racket.discourse.group/u/late_bloomer)\
**Post date:** [June 28, 2022, 3:04am UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/7 "2022-06-28T03:04:39Z")

</div>

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... 😕

@spdegabrielle : Thank you for the welcome! 🙂  
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... 😕  
Should I post the program here?

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

@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! 🙂

---

<div class="post-metadata">

**Author:** ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)\
**Post date:** [June 28, 2022, 10:47am UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/8 "2022-06-28T10:47:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![late\_bloomer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/late_bloomer/32/428_2.png) [@late\_bloomer](https://racket.discourse.group/u/late_bloomer)\
**Post date:** [June 30, 2022, 4:51am UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/9 "2022-06-30T04:51:31Z")

</div>

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!

```scheme
;; 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")

```

---

<div class="post-metadata">

**Author:** ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)\
**Post date:** [June 30, 2022, 11:15am UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/10 "2022-06-30T11:15:11Z")

</div>

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`:

```scheme
(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:

```scheme
;; 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.

---

<div class="post-metadata">

**Author:** ![late\_bloomer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/late_bloomer/32/428_2.png) [@late\_bloomer](https://racket.discourse.group/u/late_bloomer)\
**Post date:** [July 6, 2022, 3:14pm UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/11 "2022-07-06T15:14:53Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)\
**Post date:** [July 6, 2022, 3:35pm UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/12 "2022-07-06T15:35:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [July 6, 2022, 5:33pm UTC](https://racket.discourse.group/t/intervals-trivial-questions-from-an-absolute-beginner/1099/13 "2022-07-06T17:33:01Z")

</div>

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.
