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

784 B

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.

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
div num 0.0 = infinity
div num den = num / den

this is equivalent to

div num den =
  if den == 0.0 then
    infinity 
  else
    num / den