# Working Through Exercises in HtDP 2nd Edition

**URL:** <https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522>\
**Category:** Show & Tell\
**Tags:** typed-racket, htdp\
**Created:** [January 28, 2025, 9:14pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522 "2025-01-28T21:14:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![OldDutchCap](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/olddutchcap/32/2037_2.png) [@OldDutchCap](https://racket.discourse.group/u/OldDutchCap)\
**Post date:** [January 28, 2025, 9:14pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/1 "2025-01-28T21:14:19Z")

</div>

Not that I think this is anything particularly noteworthy or special but I've been working through the exercises in HtDP 2nd Edition and posting my answers up to my blog lately. I decided to try to do the exercises with Typed Racket and RackUnit (rather than using the BSL language).

> **[Exercises 80 and 81 From How To Design Programs](https://onor.io/2025/01/exercises-80-and-81-from-how-to-design-programs.html)**
>
> My responses for Exercises 80 and 81 (Chapter 5.8) of \_How to Design Programs\_...

I'm mentioning this here mainly so that if anyone cares to comment ("Hey Onorio--here's a better way to do that!") they can do so!

---

<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:** [January 28, 2025, 9:56pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/2 "2025-01-28T21:56:32Z")

</div>

Since you are using “adult Racket”, consider using `(module+ test …)` to set tests apart from code.

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [January 28, 2025, 10:03pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/3 "2025-01-28T22:03:57Z")

</div>

2 thoughts—

1. Idiomatic Racket puts test code in a test submodule. So `(module+ test …)`, and you can `(require rackunit)` inside that module, define your examples and checks there, etc.
2. You probably already know this, but time is hard. HTDP's version may be convenient for an exercise, but don't use anything like it for production code 😉 in particular, leap seconds will screw up the calculation, and there's probably other things I'm missing.

---

<div class="post-metadata">

**Author:** ![OldDutchCap](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/olddutchcap/32/2037_2.png) [@OldDutchCap](https://racket.discourse.group/u/OldDutchCap)\
**Post date:** [January 28, 2025, 10:09pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/4 "2025-01-28T22:09:01Z")

</div>

Thanks @EmEf and @benknoble for the suggestions!

---

<div class="post-metadata">

**Author:** ![OldDutchCap](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/olddutchcap/32/2037_2.png) [@OldDutchCap](https://racket.discourse.group/u/OldDutchCap)\
**Post date:** [January 28, 2025, 10:31pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/5 "2025-01-28T22:31:40Z")

</div>

When you put tests inside a submodule is the source code left in the same file? For instance on this example:

```scheme
; 5.8 Exercise 81
;time-since-midnight is (time-since-midnight Number Number Number)
(struct time-since-midnight ([hour : Number] [minute : Number] [second : Number]))

; Take TIME-SINCE-MIDNIGHT and return equivalent number of seconds
; time-since-midnight -> seconds
;
(: time->seconds (-> time-since-midnight Number))
(define (time->seconds time)
  (+ (* (time-since-midnight-hour time) 3600)
     (* (time-since-midnight-minute time) 60)
     (time-since-midnight-second time)))

(check-equal? (time->seconds (time-since-midnight 0 23 30)) 1410)
(check-equal? (time->seconds (time-since-midnight 10 12 25)) 36745)

```

Would I modify it to something like this?

```scheme
; 5.8 Exercise 81
;time-since-midnight is (time-since-midnight Number Number Number)
(struct time-since-midnight ([hour : Number] [minute : Number] [second : Number]))

; Take TIME-SINCE-MIDNIGHT and return equivalent number of seconds
; time-since-midnight -> seconds
;
(: time->seconds (-> time-since-midnight Number))
(define (time->seconds time)
  (+ (* (time-since-midnight-hour time) 3600)
     (* (time-since-midnight-minute time) 60)
     (time-since-midnight-second time)))

(module+ test
(check-equal? (time->seconds (time-since-midnight 0 23 30)) 1410)
(check-equal? (time->seconds (time-since-midnight 10 12 25)) 36745))

```

I hate to impose on your generous help but could you point me to an example of what you're talking about?

---

<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:** [January 28, 2025, 10:48pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/6 "2025-01-28T22:48:18Z")

</div>

I like to keep the dependency on rack unit also in a submodule, like this:

```scheme
#lang typed/racket

(module+ test
  (require typed/rackunit))

(struct time-since-midnight [{hours : Natural}] #:type-name TimeSinceMidnight)

(: since (TimeSinceMidnight -> Natural))
;; determine the number of minutes since midnight from `this` TimeSinceMidnight
(define (since tsm)
  (* 60 (time-since-midnight-hours tsm)))

(module+ test
  (check-equal? (since (time-since-midnight 1)) 60))

```

I also like to name the Type something different.

---

<div class="post-metadata">

**Author:** ![OldDutchCap](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/olddutchcap/32/2037_2.png) [@OldDutchCap](https://racket.discourse.group/u/OldDutchCap)\
**Post date:** [January 29, 2025, 2:49pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/7 "2025-01-29T14:49:10Z")

</div>

Thanks again for sharing!

Since you've been so kind as to share your know-how so far--is it idiomatic to keep the tests in the same source file as the code or are you simply putting them all in the same file just for purposes of illustration?

And that tip about #:type-name is very welcome as well. Some of the names I come up with are pretty clumsy; I don't think #:type-name will help with that but it's a step in the right direction.

---

<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:** [January 29, 2025, 3:24pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/8 "2025-01-29T15:24:52Z")

</div>

My own preference is to keep tests inside the same file for my course projects. (When you start writing large programs — like the ones I used to assign in our Fundamentals 1 — consider reading [https://felleisen.org/matthias/Thoughts/Modular\_Programming.html](https://felleisen.org/matthias/Thoughts/Modular_Programming.html) for this kind of programing.)

The Racket standard is to split packages into three parts: code, tests, docs so that people allergic to receiving more than absolutely necessary can just ge the code.

---

<div class="post-metadata">

**Author:** ![OldDutchCap](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/olddutchcap/32/2037_2.png) [@OldDutchCap](https://racket.discourse.group/u/OldDutchCap)\
**Post date:** [January 29, 2025, 3:38pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/9 "2025-01-29T15:38:19Z")

</div>

Thank you!

---

<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:** [February 1, 2025, 6:21pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/10 "2025-02-01T18:21:21Z")

</div>

meetings added to Racket-calendar!

https://calendar.google.com/calendar/embed?src=a6a7g9sg739gjfeak6ilkh8fdc%40group.calendar.google.com&rm=minimal

---

<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:** [February 13, 2025, 7:06pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/11 "2025-02-13T19:06:22Z")

</div>

Apologies - I was 30 minutes too late 😱

I'll catch up for 27th

---

<div class="post-metadata">

**Author:** ![OldDutchCap](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/olddutchcap/32/2037_2.png) [@OldDutchCap](https://racket.discourse.group/u/OldDutchCap)\
**Post date:** [February 13, 2025, 9:15pm UTC](https://racket.discourse.group/t/working-through-exercises-in-htdp-2nd-edition/3522/12 "2025-02-13T21:15:03Z")

</div>

No worries! We'll catch up then!
