typestar

Language basics

27 steps in 9 sets of Haskell.

Haskell is the language where the type system does the heavy lifting: pure functions by default, effects marked in the types, and a compiler that catches whole categories of bugs before anything runs. It reads less like instructions and more like definitions — a program is a set of equations, and GHC figures out how to run them lazily, only as results are demanded.

This tour covers the working core in plain GHC2021 — no extensions: bindings with signatures, currying and composition, pattern matching with guards, lists and comprehensions, the map/filter/fold trio, algebraic data types and records, typeclasses with deriving and hand-written instances, and just enough do-notation to print results. The encore ships complete programs. Every seed typechecks under ghc -Wall with warnings as errors, and the encore runs start to finish.

Start this tour

Values & types

Functions

Pattern matching

  • case and list shapesReads a corridor of traffic signals with case expressions over a sum type.
  • Guards and whereTurns a term of exam scores into a letter grade with a note attached.
  • Tuples and pairsPairs seat rows with ticket counts, then picks out the busiest row.

Lists

  • Building lists(:) adds one element to the front, (++) joins two whole lists, and a pattern covers the empty case.
  • Ranges[6 .. 10] counts up by one; naming the first two elements picks any other step.
  • List comprehensionsGenerators on the right of the bar feed the expression on the left, and guards drop what fails.

Higher-order

  • map & filtermap rewrites every element, filter keeps the ones that pass, and (.) chains the two into a pipeline.
  • Foldsfoldr and foldl' both collapse a list to one value; they differ in direction and in when work happens.
  • Lambdas & sectionsA lambda is a function with no name; a section is an operator with one operand already filled in.

Types & records

  • Sum typesA sum type lists every value the type can hold, and deriving writes the printing and comparison code.
  • RecordsRecord syntax names each field, defines an accessor per field, and copies a value with changes.
  • Maybe & EitherMaybe reports a missing value as Nothing; Either carries a reason on the Left.

Typeclasses

  • Deriving instancesA deriving clause on a sum type supplies Show, Eq, Ord, Enum, and Bounded for free.
  • Custom instancesHand-written Show and Eq instances decide how a value prints and what counts as equal.
  • Ord and sortingThree orderings of one list: sortBy with a comparator, sortOn with a key, and Down to reverse.

IO basics

  • Sequencing IO with doA do block runs IO actions top to bottom, calling a pure helper partway through.
  • let versus bind in IOInside do, let names a pure value while <- runs an action and binds its result.
  • Printing with mapM_mapM_ runs an action per element and drops the results; forM_ is the same with flipped arguments.

Encore

  • word_freq.hsCounts word frequencies across an inline passage and prints the top eight with totals.
  • grade_report.hsAverages each student's scores, assigns a letter grade, and prints a ranked table with a class summary.
  • temp_stats.hsReports a week of daily highs with the low, high, mean, and a bar chart beside each day.

The other Haskell tours