Haskell Tutorial
Learn Haskell step by step: 57 lessons in 11 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting started
- Introduction to Haskell Meet Haskell, a purely functional language with static types and lazy evaluation. See what makes it different, where it is used, and how this course runs code.
- Installing GHC and Using GHCi Install the Haskell toolchain with GHCup, then learn the GHCi commands :t, :i, :r and :l, how to use runghc, and what Cabal and Stack are for.
- Your First Haskell Program Write and compile a Haskell program: main, putStrLn, do blocks, the module header, comments and the layout rule that uses indentation instead of braces.
- Expressions, Functions and Application Learn how Haskell treats everything as an expression, how to call and define functions without parentheses, use infix operators and write operator sections.
Types and values
- Basic Types and Type Signatures Learn Haskell's basic types Int, Integer, Double, Bool, Char and String, how to read type signatures like a -> b -> c, and how type inference fills gaps.
- Type Classes: Eq, Ord, Show, Read, Num Understand Haskell type classes and constraints like Eq a =>, learn Eq, Ord, Show, Read and Num, use deriving, and see how numeric defaulting works.
- Tuples and Lists Learn Haskell tuples and lists: fixed-size tuples with fst and snd, list literals, the cons operator, ranges, and head, tail, take and drop.
- List Comprehensions Build Haskell lists the way mathematicians write sets: generators, guards, nested generators, let bindings and pattern filters, with examples.
- Strings, Chars and Text Understand Haskell String as a list of Char, use Data.Char, show and read, words and lines, and learn when to reach for Data.Text instead.
Functions
- Pattern Matching Learn Haskell pattern matching: literals, tuples, lists, wildcards, as-patterns, case expressions and what happens with non-exhaustive patterns.
- Guards, where and let Write conditions with Haskell guards and if-then-else, then name intermediate values with where and let. Includes otherwise and MultiWayIf.
- Recursion Learn recursion in Haskell: base and recursive cases, accumulators, tail recursion, and how laziness and strictness affect stack and memory use.
- Currying and Partial Application Learn why every Haskell function takes one argument, how partial application and operator sections work, and when point-free style helps or hurts.
- Higher-Order Functions: map, filter, folds Learn Haskell higher-order functions: map, filter, foldr, foldl, foldl', zip, zipWith and iterate, with clear examples and runnable JavaScript stand-ins.
- Lambdas, Composition and the $ Operator Learn Haskell lambda syntax, the composition operator (.), the application operator ($), flip and how to build readable function pipelines.
Defining types
- Algebraic Data Types Learn how to define your own Haskell types with data: constructors, sum and product types, records, deriving and pattern matching on constructors.
- Records, newtype and Type Synonyms Learn Haskell record syntax and updates, field accessors, the difference between newtype, data and type, and when to use strict fields.
- Maybe and Either Learn how Haskell models missing values with Maybe and failures with Either, using fromMaybe, maybe, either, chaining and total functions.
- Recursive Data Types and Trees Learn how Haskell types can refer to themselves: build lists, binary search trees and expression trees, then evaluate and fold over them.
- Parametric Polymorphism and Type Variables Understand Haskell type variables, parametric polymorphism, what a signature like a -> a can and cannot do, and how class constraints narrow it.
- Defining Your Own Type Classes and Instances Learn to declare a Haskell type class, write instances, use default methods and deriving, and avoid orphan instances, with a runnable JavaScript stand-in.
Laziness and purity
- Lazy Evaluation Learn how Haskell delays work until a value is needed: thunks, call by need, infinite lists with take, iterate and cycle, and when laziness helps.
- Strictness, Bang Patterns and seq Learn why lazy accumulators leak memory in Haskell and how foldl', seq, bang patterns, strict fields and deepseq force evaluation at the right time.
- Purity and Referential Transparency Understand pure functions, referential transparency and equational reasoning in Haskell, and why purity makes code easier to test, refactor and optimise.
Functors, applicatives and monads
- Functors Understand the Haskell Functor class: how fmap and <$> apply a function inside Maybe, lists and functions, and which laws every instance must obey.
- Applicative Functors Learn Haskell's Applicative class: pure, <*>, liftA2 and applicative style for combining independent wrapped values, such as validating form fields.
- Monads A clear, myth-free introduction to Haskell monads: the >>= operator, Maybe and list monads, the three monad laws, and a runnable JavaScript stand-in.
- do-Notation Demystified See how Haskell do blocks desugar into >>= and >>, how let and pattern matching work inside do, and why do is not just for IO.
- Semigroups and Monoids Learn Haskell's Semigroup and Monoid classes: combining values with <>, identity elements, Sum, Product, Any, All, mconcat and foldMap, with examples.
- Foldable and Traversable Learn Haskell's Foldable and Traversable classes: fold any container, then use traverse, sequenceA and mapM to run effects such as validation over a list.
- State, Reader, Writer and Monad Transformers Learn the State, Reader and Writer monads, then stack effects with StateT and ExceptT and the mtl style. Includes a runnable State stand-in.
IO and the real world
- IO Basics Learn how Haskell handles input and output: the IO type, getLine, putStrLn, return versus pure, sequencing, interact and why IO actions are values.
- Files, Arguments and Environment Read and write files in Haskell with readFile, writeFile and withFile, handle getArgs and exit codes, and avoid the lazy IO trap.
- Exceptions and Error Handling Handle failure in Haskell: choose between Maybe, Either and exceptions, use throwIO, catch, try, bracket and ExceptT, and define custom exceptions.
- Mutable State: IORef, MVar and ST Learn when and how to use mutable variables in Haskell: IORef and modifyIORef', MVar for sharing, and the ST monad with STRef and STUArray.
Modules, packages and tooling
- Modules and Imports Organize Haskell code with modules: the module header, export lists, qualified imports, hiding Prelude names and fixing ambiguous name clashes.
- Cabal, Stack and Packages Create a Haskell project with Cabal or Stack, read a .cabal file, add dependencies from Hackage and Stackage, and learn the standard project layout.
- Essential Libraries: containers, text, bytestring, vector Meet the Haskell libraries almost every project uses: Data.Map and Data.Set, Data.Sequence, Text, ByteString and Vector, and learn how to pick the right one.
- Testing: HUnit, Hspec and QuickCheck Test Haskell code with HUnit and Hspec for examples and QuickCheck for properties, and learn how generators, shrinking and doctest fit together.
- Debugging, Profiling and Performance Find bugs and slow code in Haskell: Debug.Trace, GHC warnings with -Wall, HLint, GHCi breakpoints, profiling, strictness, list fusion and criterion benchmarks.
Advanced types
- GHC Extensions Worth Knowing Learn what LANGUAGE pragmas do and which GHC extensions are worth knowing: LambdaCase, TupleSections, RecordWildCards, OverloadedStrings and TypeApplications.
- GADTs, Type Families and DataKinds Use GADTs for a type-safe expression evaluator, type families to compute types, and DataKinds with phantom types to make illegal states unrepresentable.
- Lenses and Optics Learn what Haskell lenses are: getters and setters as composable values, view, over and set, the lens and optics libraries, plus a runnable JavaScript stand-in.
- Effect Systems and Free Monads Understand how Haskell separates what a program does from how it runs: tagless final, free monads, and effect libraries, with a runnable JavaScript stand-in.
- Concurrency and Software Transactional Memory Learn Haskell concurrency: lightweight threads with forkIO, MVar as a lock and channel, STM and TVar for safe shared state, async, and parallelism with par.
Real-world Haskell
- Parsing with Parser Combinators Build a Haskell parser from small functions: Parser as a function, Functor, Applicative and Alternative instances, an expression grammar, and megaparsec.
- JSON and Web Services Encode and decode JSON in Haskell with aeson and generics, then see how Scotty and servant build web services and how wreq calls other APIs.
- Project: Expression Calculator Build an arithmetic calculator in Haskell in three stages: a tokenizer, a parser into an expression tree, and an evaluator with Either errors, plus tests.
- Project: Command-Line Todo App Build a todo manager in Haskell: records, a pure core, file persistence, optparse-applicative commands and Hspec tests, with a runnable JS model.
- Project: Bank Account Simulator with State Build a bank account simulator in Haskell with Either errors, the State monad and transaction replay, plus a runnable JavaScript model.
- Project: JSON Parser from Scratch Build a JSON parser and pretty printer in Haskell with a small parser-combinator library, then test a round trip. Includes runnable JS models.
- Project: Text Adventure Engine Build a text adventure in Haskell: the world as data, a command parser, a pure step function and a thin IO loop, with a runnable JavaScript model.
Practice and interviews
- Practice: Functions, Lists and Recursion Fifteen Haskell exercises with solutions on map, filter, folds, recursion and lazy lists, plus runnable JavaScript checks to test your ideas.
- Practice: Types, Classes and Monads Twelve Haskell exercises with solutions on algebraic data types, Maybe and Either, type classes, folds and monad chaining, with runnable JS checks.
- Common Mistakes and Error Messages Learn the Haskell mistakes beginners hit most: type errors, layout problems, ambiguous types, partial functions and space leaks, and how to read GHC messages.
- Haskell Interview Questions Prepare for Haskell interviews with short, accurate answers on laziness, purity, type classes, monads, folds, GHC and tooling, plus runnable stand-ins.
- Haskell Cheat Sheet A compact Haskell cheat sheet: syntax, Prelude functions, type classes, monad idioms, GHCi commands and common GHC extensions, with links to each lesson.