Haskell from the ground up
Values, functions and types that keep their promises
- Beginner
- 12 chapters
- 206 min read
Learn Haskell by writing it: values and types, functions you define yourself, lists and the patterns that take them apart, recursion in place of loops, map and filter and the fold, tuples and zip, list comprehensions, Maybe for searches that can fail, types of your own with the abilities you ask for, Either for failures that explain themselves, and a sales report at the end that uses all of it.
What You'll Learn
- Write a program as the value main and print lines of output with it
- Tell the small types apart and turn any value into text with show
- Define a function with a type line that promises what goes in and out
- Build a list with the colon and take it apart by matching on its shape
- Choose an answer with guards, and know why if needs an else branch
- Repeat work by calling a function on a smaller piece, with a base case
- Name helpers and values close to where they are used with where and let
- Replace hand-written walks with map, filter and foldr
- Bundle values as tuples and walk two lists side by side with zip
- Describe a list you want with a comprehension and a condition
- Answer a search that can fail with Maybe, and say why it is not null
- Give your own data a type, ask for Show, Eq and Ord, and sort by a field
- Report a failure with Either, carrying the reason back to the caller
- Build a formatted report from records, a sort and a set of totals
Prerequisites
- No programming experience needed — the first chapter starts at the first value
- If you know another language, the biggest difference is that nothing here changes state
Course Content
- 01Values, types and the first programNot started
What runs when you press Run, how a value becomes text with show, and the small set of types every line in this course is built from.
- 02Functions you write yourselfNot started
How a function definition is written, how you apply a function to its arguments, and why a type line is a promise worth writing down.
- 03Lists, and matching on their shapeNot started
The list as the one collection in the language, the colon that builds it, the pattern that takes it apart, and why text is a list of characters.
- 04Guards and readable choicesNot started
Yes and no values, comparisons, and the two ways to choose an answer: a chain of guards or a single if expression.
- 05Recursion is the loopNot started
There is no for and no while: a function calls itself on a smaller piece of the work until a base case answers, with where naming the helpers.
- 06map, filter and the foldNot started
Functions are values you can pass around, so three small helpers replace most hand-written walks: change every value, keep some, or boil a list down to one answer.
- 07Tuples, zip and paired dataNot started
Bundling a few values together when they belong together, walking two lists side by side with zip and zipWith, and reading the halves of a pair.
- 08List comprehensionsNot started
Describing the list you want instead of the steps that build it: a generator says where the values come from, and a condition keeps some of them.
- 09Maybe: when there may be no answerNot started
A lookup that can fail should say so in its type: Maybe holds either nothing at all or a real answer, and you take it apart before you use it.
- 10Your own types, and what deriving gives youNot started
data builds a type of your own out of named fields, deriving asks for the abilities the type should have, and a lowercase name in a signature means any type at all.
- 11Either: errors that carry a messageNot started
When a failure needs to explain itself, Either brings the reason back as a value: one half for the problem, one half for the answer, both written in the type.
- 12Capstone: a sales report from recordsNot started
A report built from everything in the course: records with a derived order, a sort by one field, totals that aggregate, and formatted lines printed by the map that runs an action.
