Haskell Book: Chapters 1 & 2
Contents
So this morning I got started with the Haskell book. Here are some notes and exercises.
Chapter 1
This is just a very short and simple introduction to the lambda calculus.
- Syntax of λ-calculus
|
|
- α-equivalence: basically the notion that $\lambda{x}.x$, $\lambda{y}.y$ and $\lambda{z}.z$ all express the same function.
- β-reduction: when you apply a function to some expression you replace all bound occurrences in the body with that expression and eliminate the head. To indicate that we are substituting e.g. $x$ with $z$ in $\lambda{x}.x$ we write: $$[x := z]$$
- Applications are left-associative: $$(\lambda{x}.x)(\lambda{y}.y)z$$ is equivalent to $$((\lambda{x}.x)(\lambda{y}.y))z$$
- Each lambda can only bind one parameter and can only accept one argument. Functions that require multiple arguments have multiple, nested heads. This is known as currying
- A combinator is a lambda term with no free variables
- β normal form. Beta normal form is when you cannot beta reduce (apply lambdas to arguments) the terms any further. This corresponds to a fully evaluated expression, or, in programming, a fully executed program.
- Some expressions diverge, e.g. not all reducible lambda terms reduce to a normal form. This isn’t because they’re already fully reduced, but because they diverge. E.g. the omega expression (𝜆𝑥.𝑥𝑥)(𝜆𝑥.𝑥𝑥) diverges.
semantically, Haskell is a lambda calculus. Actually, Haskell is a typed lambda calculus—more on types later— with a lot of surface-level decoration sprinkled on top, to make it easier for humans to write, but the semantics of the core language are the same as the lambda calculus. That is, the meaning of Haskell programs is centered around evaluating expressions.
Exercises
Chapter 2
|
|
Most of this wasn’t new. But it pushed me to clarify what is meant by “pure” function which is a somewhat confusing notion. So all of these are impiure functions in Python.
|
|
Other than that:
- REPL +
ghci
- infixing with backticks and prefixing with parens
- association and precedence (with
:info
) let
vswhere