Why does the function product
in Haskell return 1
if it is given an empty list?
Lists form a monoid structure, with associative binary operation ++
and neutral element []
. That is, we have
[] ++ xs = xs = xs ++ [] (xs ++ ys) ++ zs = xs ++ (ys ++ zs)
Meanwhile, numbers have lots of monoid structure, but the relevant one here is that where the operation is *
and the neutral element is 1
.
1 * x = x = x * 1 (x * y) * z = x * (y * z)
The product
function is not only a map from lists of numbers to numbers: it's a monoid homomorphism, reflecting the list monoid structure in the numerical monoid. Crucially,
product (xs ++ ys) = product xs * product ys
and
product [] = 1
In fact, to get the former, we pretty much have the latter forced upon us.
Because that's the identity in the category of multiplication.
To be more practical:
product [1,2,3] == 1 * product 2:3:[]
== 1 * 2 * product 3:[]
== 1 * 2 * 3 * product []
Which in turn allows you to implement it with a simple recursion:
product [] = 1
product (x:xs) = x * product xs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With