I’ve got a physical copy of HTDP 2001. A copy of this book is accessible online along with the answer key.
Notes on MIT’s “How To Design Programs” (HTDP) 1e
Chapter summaries and important notes from my own learning.
I Processing Simple Forms of Data
1 Students, Teachers, and Computers
Some programs are as small as essays. Others are like sets of encyclopedias. Writing good essays and books requires careful planning, and writing good programs does, too. Small or large, a good program cannot be created by tinkering around. It must be carefully designed.
2 Numbers, Expressions, Simple Programs
2.1 Numbers and Arithmetic
2.2 Variables and Programs
2.3 Word Problems
2.4 Errors
2.5 Designing Programs
Scheme uses parentheses ( ) or round brackets to organize programs. A set of parentheses encloses an expression.
; Exercise 2.2.1. Define the program Fahrenheit->Celsius,; which consumes a temperature measured in Fahrenheit and; produces the Celsius equivalent. Use a chemistry or; physics book to look up the conversion formula.(define (fahrenheit->celsiusx)
(* (- x32) (/ 59)))
(convert-guifahrenheit->celsius)
Scheme allows you to represent fractions directly as well:
(define (fahrenheit->celsiusx)
(* (- x32) 5/9 ))
…though I’m not a huge fan of that.
Inexact numbers are represented with an #i in front.
Here’s a word problem and my solution:
Company XYZ & Co. pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work.
My solution is slightly more efficient than the solution as it does not recompute the gross pay by including the subtract-taxes function.
Designing programs is something that must be done intentionally and with care. SICP provides a Design Recipe which can be followed to yield reliably useful programs.
We need to determine what’s relevant in the problem statement and what we can ignore. We need to understand what the program consumes, what it produces, and how it relates inputs to outputs. We must know, or find out, whether Scheme provides certain basic operations for the data that our program is to process. If not, we might have to develop auxiliary programs that implement these operations. Finally, once we have a program, we must check whether it actually performs the intended computation.
;; Contract: area-of-ring : number number -> number;; Purpose: to compute the area of a ring whose radius is;; outer and whose hole has a radius of inner;; Example: (area-of-ring 5 3) should produce 50.24;; Definition: [refines the header](define (area-of-ringouterinner)
(- (area-of-diskouter)
(area-of-diskinner)))
;; Tests:(area-of-ring53)
;; expected value50.24
A summary of these phases is provided in Figure 4 in this section of the textbook, but I’ll summarize here quickly:
The Contract names the function and specifies the class of the input and output data
The Purpose explains the name
The Example lists a sample use case
The Definition is a scheme expression
Finally, Tests ensure output is correct
(Funny, Elixir has real good support for inline tests with the doctest feature. Makes me miss Elixir.)
3 Programs are Function Plus Variable Definitions
3.1 Composing Functions
3.2 Variable Definitions
3.3 Finger Exercises on Composing Functions
4 Conditional Expressions and Functions
4.1 Booleans and Relations
4.2 Functions that Test Conditions
4.3 Conditionals and Conditional Functions
4.4 Designing Conditional Functions
5 Symbolic Information
5.1 Finger Exercises with Symbols
6 Compound Data, Part 1: Structures
6.1 Structures
6.2 Extended Exercise: Drawing Simple Pictures
6.3 Structure Definitions
6.4 Data Definitions
6.5 Designing Functions for Compound Data
6.6 Extended Exercise: Moving Circles and Rectangles
6.7 Extended Exercise: Hangman
7 The Varieties of Data
7.1 Mixing and Distinguishing Data
7.2 Designing Functions for Mixed Data
7.3 Composing Functions, Revisited
7.4 Extended Exercise: Moving Shapes
7.5 Input Errors
8 Intermezzo 1: Syntax and Semantics
8.1 The Scheme Vocabulary
8.2 The Scheme Grammar
8.3 The Meaning of Scheme
8.4 Errors
8.5 Boolean Expressions
8.6 Variable Definitions
8.7 Structure Definitions
II Processing Arbitrarily Large Data
9 Compound Data, Part 2: Lists
9.1 Lists
9.2 Data Definitions for Lists of Arbitrary Length
9.3 Processing Lists of Arbitrary Length
9.4 Designing Functions for Self-Referential Data Definitions
9.5 More on Processing Simple Lists