I - Fixed-Size Data (HTDP)

alex 15th September 2024 at 2:13pm

This was a very thorough chapter on how to get started in programming. It includes the very basics (function and data definitions), with emphasis on fixed-size data (think integers, strings, booleans, structs, etc., in opposition to lists).


I - 2 Functions and Programs

2.2 Composing Functions

Define one function per task.

2.4 Global Constants

For every constant mentioned in a problem statement, introduce one constant definition.

I - 3 How to Design Programs

3.1 Designing Functions

The book establishes the relationship between information and data. Information belongs to the real world — it is then converted into data, which is its representation in a given program.

It expands on the design process, which itself was already established in the basic steps to program design.

1. Express how you wish to represent information as data.

A one-line comment suffices:

; We use numbers to represent centimeters.

It is also possible (and recommended) to state a data definition:

; A Temperature is a Number. 
; interpretation represents Celsius degrees
; An LR (short for launching rocket) is one of:
; – "resting"
; – NonnegativeNumber
; interpretation "resting" represents a grounded rocket
; a number denotes the height of a rocket in flight

2. Write down a signature, a statement of purpose, and a function header.

A function signature is a comment (akin to type hints in Python)

; String -> Number

A purpose statement summarizes the purpose of the function (ideally, in a single line, answering the question what does the function compute?).

Every reader of your program should understand what your functions compute without having to read the function itself.

A header is a simplistic function definition.

3. Illustrate the signature and purpose statement with some functional examples.

; Number -> Number
; computes the area of a square with side len 
; given: 2, expect: 4
; given: 7, expect: 49
(define (area-of-square len) 0)

(I usually do this via unit-tests, before programming the function bodies, as per test-driven development practices; in fact, shortly after the book establishes the existence of test frameworks, and how those can replace the given/expect strings).

4. Take inventory.

Distinguish what are the givens, and what needs to be computed.

5. Do some code!

6. Test the function on the examples given

(but this, via the unit-tests, is already done).

An example:

; String -> String
; returns the argument without the first character.
; given "Alex", expect: "lex";
; given ""; expect: "";
(define (string-rest s)
  (cond [(eq? (string-length s) 0) ""]
        [else (substring s 1)]))

(string-rest "")
(string-rest "Alex")

I - 4 Intervals, Enumerations, and Itemizations

4.5 Intervals, Enumerations, and Itemizations

This chapter handles the construction of conditional statements, establishing that these can be defined in terms of:

- intervals (eg. a range of values)

; WorldState -> WorldState
(define (g y)
  (cond
    [(<= 0 y CLOSE) ...]
    [(and (< CLOSE y) (<= y HEIGHT)) ...]
    [(> y HEIGHT) ...]))

- enumerations (specifying all possible values)

; TrafficLight -> TrafficLight
; yields the next state given current state s
(check-expect (traffic-light-next "red") "green")
(define (traffic-light-next s)
  (cond
    [(string=? "red" s) "green"]
    [(string=? "green" s) "yellow"]
    [(string=? "yellow" s) "red"]))

- itemization (handling specific cases, and then resorting to ranges; thus, a mix of the two prior).

In the following definition of string->number,

; String -> NorF
; converts the given string into a number;
; produces #false if impossible 
(define (string->number s) (... s ...))

`NorF` is an itemized definition.

```scheme
; An NorF is one of: 
; – #false
; – a Number

(considering that #false is a single piece of data, and Number is a whole class of possible values)

4.7 Finite State Worlds

In many situations, state transition diagrams have only a finite number of states and arrows. Computer scientists call such diagrams finite state machines (FSM), also known as finite state automata (FSA). Despite their simplicity, FSMs/FSAs play an important role in computer science.

A few years ago, I had a university course on finite state automata, but don't remember much. I've also seen some solutions to this year's Advent of Code.

In this chapter there's a very nice data definition; I should use patterns like such more often.

(define RED 0)
(define GREEN 1)
(define YELLOW 2)
 
; An S-TrafficLight is one of:
; – RED
; – GREEN
; – YELLOW