2 ms·
> ...there is only one way to write something There are several such examples in functional programming languages. Consider the Haskell function `map`, that ta
by majewsky 9y ago
> ...there is only one way to write something
There are several such examples in functional programming languages. Consider the Haskell function `map`, that takes a function and a list, and applies the function to each element of the list:
map :: (a -> b) -> [a] -> [b]
Keep in mind that this function is pure, so you cannot get any values into the function from outside via side effects. So if you have a call like `map f []` (i.e. the list of input values of type `a` is empty), there are no values of type `a` anywhere that you can use, hence you cannot call the function `f`, and hence you cannot have any values of type `b` either. Therefore, literally the only possible result is
map f [] = []
By a similar argument, you can see that the alternative case for non-empty lists can only be
map f (x:xs) = (f x) : (map f xs)
Source: https://wiki.haskell.org/Typeclassopedia#Laws https://wiki.haskell.org/Typeclassopedia#Laws (contains a citation to the relevant paper by Philip Wadler)