<aside> 💡 The core idea of the HtDD recipe is this:
When identified, the structure of the information
gives us the structure of the data representing this information, which
gives us the structure of a function processing this data, and
guides the creation of tests (check-expect
) for the function.
∴ remember that identifying the structure of the information in the problem domain is a crucial first step in systematic program design.
</aside>
cond
expressionsThe cond
keyword is a multi-armed conditional — it’s a highly readable way to run multiple conditionals in parallel (like an if
-elif
-else
structure in Python).
Its structure is as follows:
(cond [(condition) result] ; if
[(condition) result] ; elif
[(condition) result] ; elif
...
[else (answer)]) ; else clause is optional
If the topmost condition evaluates to false
, it gets “deleted” and we move on to the next statement. This process repeats until 1 condition evaluates to true
. When that happens, the result corresponding to the true
condition is returned.
(cond [(> 1 2) "bigger"]
[(= 1 2) "equal"]
[(< 1 2) "smaller"])
---------------------------------------------------------------------------
(cond [false "bigger"] ; false -> condition will be deleted
[(= 1 2) "equal"]
[(< 1 2) "smaller"])
---------------------------------------------------------------------------
(cond [(= 1 2) "equal"]
[(< 1 2) "smaller"])
---------------------------------------------------------------------------
(cond [false "equal"] ; false -> condition will be deleted
[(< 1 2) "smaller"])
---------------------------------------------------------------------------
(cond [(< 1 2) "smaller"])
---------------------------------------------------------------------------
(cond [true "smaller"]) ; true -> result will be returned
---------------------------------------------------------------------------
"smaller"
Data definitions are parts of programs which describe
Data definitions make programs simpler to understand and simplifies the design of functions involving the newly-defined data.