How to Design Programs#

The Book#

The How to Design Programs Book (HtDP) is the book that has most changed the way I think about, see and do programming. I have previously gone through the How to Code: Systematic Program Design Course on EDX (part 1, 2 and 3, free, later updated and renamed to How To Code) which is based on the HtDP book.

During the course – which also meant I perused the book a lot – I learned in practice what it means to use TDD to guide the design of programs (together with other concepts), and how tests serve as specification and documentation as much as they serve as tests proper.

This is the number one book I recommend to my friends (online or otherwise), coworkers, alien species, or other creatures I have the chance to do so 😅. By the way, the authors of the book sometimes answer questions on the racket-users google group.

I’m slowly (as time permits) studying the book (not the course this time). My previous solutions from the course exercises are in my htcspd gitlab repository.

There is also the HtDP Plus page with additional material that further extends the topics covered in the book.

Design Recipes and Examples#

Racket and BSL#

  1. Add the repos to .emacs or init.el as described in

  2. Run M-x RET package-refresh-contents RET (they don’t mention it

    in the tutorial).

  3. Run M-x RET package-install geiser RET.

Geiser info pages:

C-h i m Geiser RET

Read about installation notes for Chicken Scheme stuff.

M-x run-geiser

Open a .rkt file in Emacs. Whether you have Geiser repl running or not, hit C-c C-a and all functions and other definitions inside the .rkt file will be available for use in the Geiser REPL.

In geiser REPL you can do

(enter! "foo.rkt")
(enter! "projs/main.rkt")

to load code on those files into the REPL. It seems the files must start with ``#lang racket`` or something like that. Or, if you are editing a .rkt file and want to “enter” it into the REPL:

C-c C-a

Teaching Languages — htdp/bsl 📖#

#lang htdp/bsl

Topic about images in htdp/bsl that shows some useful things.

To run tests like those with check-expect, your .rkt file must start with one of these (beginner/intermediate/advanced student languages. The “+” a few more features.

#lang htdp/bsl
#lang htdp/bsl+
#lang htdp/isl
#lang htdp/isl+
#lang htdp/asl


#reader(lib "" "lang")((modname area-tests) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))

Emacs, Geiser#

C-u C-c C-z or C-c C-a brings one to the REPL in the current module/file. It DISPLAYS IMAGES! 💖️

C-c C-k runs fine with the http languages (C-c C-a seems not work for the teaching languages).

But not both.

Then, from racket-mode, do C-c C-t (runs racket-test and be happy. From the command line,

$ raco test file.rkt

Racket Mode#

Docs for thing at point: C-c C-d. Insert lambda: C-m-y.

Other libs used in the book#


C-x o to move from interactions to definition window.

TIP: To use (bitmap "../images/foo.png") make sure the file is saved so the relative path works, otherwise, with an unsaved buffer, DrRacket will try to load images relative to your home directory.

WARNING: If you paste/insert images through Racket’s UI, or your you set the language through the UI, you end up with unreadable source code files. Use a header like one of these and see the next tip about “Determine language from source”.

TIP: To allow the line #lang htdp/<some language>, make sure “determine language from source” is selected in DrRacket’s bottom left corner.

TIP: On wide screens, you may find it useful to display the interactions pane on the right side (instead of at the bottom). Go to “View → Use Horizontal Layout”. There is a setting in Preferences too.

Naming Source Files#

Name each source file incrementally and append a descriptive name:

e001e-function.rkt (example from the book, note “e???e…”) e002e-images.rkt e070p49-rocket-cond.rkt e071p50-tests-traffic-light.rkt

“n” is the exercise number, like “e070p49-rocket-cond.rkt”, meaing we are at the file 70 written so far, but the practice exercise is 49.

For “World Programs”, use a “wp” as well, like in “e075p51-wp-traffic-light.rkt”.


Other People’s Notes and Solutions#

Page updated on <2021-09-27 Mon>.