November 2015
Intermediate to advanced
166 pages
3h 14m
English
Algebraic types give us a very concise way to model composite types, even recursive ones. Pattern matching makes it easy to work with algebraic types. Type classes enable both the fundamental types of polymorphism: parametric and ad-hoc.
Together, these capabilities allow us to easily express many of the Gang of Four patterns.
Algebraic data types can express a combination of types, for example:
type Name = String
type Age = Int
data Person = P String Int -- combinationThey can also express a composite of alternatives:
data MaybeInt = NoInt | JustInt Int
Here, each alternative represents a valid constructor of the algebraic type:
maybeInts = [JustInt 2, JustInt 3, JustInt 5, ...
Read now
Unlock full access