arliss_obsidian/fp/Language/Functions/Defining/Pattern Matching.md
Orion Kindel 3701b1e2f8
update
2024-09-23 18:56:55 -05:00

29 lines
784 B
Markdown

[[Functions]] may have multiple implementations that change behavior based on the shape of the arguments.
> [!note]
> You can always replace this with a [[case .. of|case expression]].
This is _similar_ to method overloading in OOP languages, but differs in that the number of arguments and type of the arguments must be the same for all implementations.
Functions can have any number of implementations, as long as all possible inputs are covered exhaustively.
e.g.
this implementation of [[Number]] division has 2 paths:
- when the denominator is zero, yields `Infinity`
- otherwise, performs the division
```haskell
div num 0.0 = infinity
div num den = num / den
```
this is equivalent to
```haskell
div num den =
if den == 0.0 then
infinity
else
num / den
```