Haskell is a functional programming language designed around mathematics, strong typing, and writing programs using functions.
Key characteristics
- Pure functional programming — functions generally avoid changing state or modifying data.
- Strong static typing — many errors are caught by the compiler before the program runs.
- Type inference — you usually don't have to explicitly specify types.
- Lazy evaluation — expressions are evaluated only when their results are needed.
- Immutable data — values don't normally change after they're created.
- Higher-order functions — functions can accept and return other functions.
- Pattern matching — powerful way to work with structured data.
- Algebraic data types — lets you model complex domains cleanly.
- GHC — the Glasgow Haskell Compiler is the primary Haskell compiler.
A simple example
Output:
The first line:
means:
square takes an Int and returns an Int.
Haskell's functional style
Instead of writing a traditional loop:
Haskell commonly expresses the operation directly:
Or transformations:
produces:
Filtering:
produces:
Why Haskell is interesting to a C++ programmer
Haskell is particularly interesting if you're coming from C++ because it approaches several familiar problems very differently.
| C++ | Haskell |
|---|
| Mutable variables | Immutable values |
| Classes/objects | Algebraic data types + typeclasses |
| Templates | Parametric polymorphism |
| RAII | Garbage collection + controlled effects |
| Exceptions | Explicitly modeled effects/errors |
| Imperative loops | Higher-order functions |
| Compile-time templates | Powerful type system |
std::optional | Maybe |
std::variant | Algebraic data types |
std::function | First-class functions |
One particularly important concept is that pure computation and side effects are separated. Haskell uses abstractions such as IO to represent operations involving the outside world.
For example:
The program can still perform I/O, but the type system makes the boundary between pure code and effects explicit.
What is Haskell used for?
Haskell has been used in:
- compilers
- financial systems
- formal methods
- distributed systems
- research
- language implementation
- data processing
- high-assurance software
- developer tooling
It isn't nearly as common commercially as C++, Java, Python, or JavaScript, but it has had a large influence on modern programming language design.
If you're interested in programming-language concepts, Haskell is especially valuable because ideas that now appear in Rust, modern C++, Swift, Scala, and functional features of many languages have strong connections to concepts developed or popularized in the functional-programming world.