1.2 KiB
1.2 KiB
Haskell Algebraic Data Types
This note covers 08-haskell-adt/, which models a build plan with sum types, a record type, and pattern matching.
1. Why This Example Matters
Haskell programs often start by turning vague strings into precise domain types.
This example does that with:
Targetas a sum type,Modeas a sum type,Outputas a sum type, andBuildPlanas a product type with record fields.
That is one of the most important intermediate Haskell habits: model the domain first, then write functions over the constructors.
2. Pattern Matching in Two Places
The example uses pattern matching in both parsing and behavior:
parseTarget,parseMode, andparseOutputturn strings into constructors, anddescribePlanmatches on theBuildPlanvalue to decide what to print.
That shows two common styles:
- pattern matching on one constructor at a time in small helper functions, and
- pattern matching on a whole record value when several fields matter together.
3. Commands to Try
cd 08-haskell-adt
nix develop
cabal run
cabal run -- executable release quiet
cabal test
nix build
./result/bin/mini-plan executable release quiet
nix run . -- executable release quiet
nix flake check