Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does product [] return 1?

Tags:

haskell

Why does the function product in Haskell return 1 if it is given an empty list?

like image 610
developerbmw Avatar asked Nov 16 '15 09:11

developerbmw


2 Answers

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.

like image 93
pigworker Avatar answered Oct 12 '22 09:10

pigworker


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
like image 23
Bartek Banachewicz Avatar answered Oct 12 '22 07:10

Bartek Banachewicz