Recursion
Statically defined cases in a recursive function are known as edge conditions. For example, if you have a function that sums a list of numbers recursively up to a limit you can define an edge condition that stops the recursively loop and returns a final value. In the example below the edge condition occurs when the summing value passed into f
reaches 1
at which point the function returns.
f :: (Eq a, Num a) => a -> a
f 1 = 1
f x = x + sumf (x - 1)
Edge Cases
Edge cases often return the identity element of the output in question. For example, in a function that sums a list of integers the identity would be an 0
because this is the additive identity of addition.
lsum :: (Num a) => [a] -> a
lsum [] = 0 -- Edge case returns additive identity
lsum (x:xs) = x + lsum xs