Algebraic Data Types (ADT)
Algebraic data types (ADTs) are composite types in programming that combine
other types using sum (OR) and product (AND) operations. ADTs are prominent in
functional languages like Haskell, OCaml, and Scala.
The types are:
- Product Type: A type containing multiple types (structs, tuples, classes)
- Sum Type: Type that is exactly one of several types (variants, tagged unions)
- Recursive Types: Types that contain themselves (linked lists, trees)
ADTs benefit from pattern matching (e.g., ensuring all variants are handled in a sum type).
Product Types
Product types (like structs, tuples or records) have an AND relationship. A
product type's number of possible values is the product of its types.
Sum Types
Sum types have an OR relationship: Their value is either Type A OR Type B, etc.
The total number of possible values is the sum of the possible values in each
type - hence them being called "sum" types.