-- COMP1100 S1, 2006

-- Lab week 4 solution

-- Students using guards should be "encouraged" to use patterns instead.
-- Frown on this:

yuckyProduct x
    | null x     = 1
    | otherwise  = head x * yuckyProduct (tail x)


-- The only bit that is even remotely interesting is to choose the
-- unit of (*) (i.e., 1) as the product of the empty list.

myProduct :: [Int] -> Int
myProduct []     = 1
myProduct (n:ns) = n * myProduct ns

-- A better type is Num a => [a] -> a

-- The standard product function gives 1 as result for []
-- An alternative where it's erroneous:

myProduct' []     = error "product of an empty list undefined"
myProduct' [n]    = n
myProduct' (n:ns) = n * myProduct' ns

