Let's say want a function, taking a list of numbers, that returns the sum of adjacent pairs of numbers, except at the two edges where it simply returns the edge numbers. For example:
[1,2,3,4,5] --> [1,3,5,7,9,5]
I come up with the following function:
p1 :: Num a => [a] -> [a]
p1 [] = []
p1 (x:xs) = x:p2 (x:xs)
p2 :: Num a => [a] -> [a]
p2 [x] = [x]
p2 (x1:x2:xs) = (x1+x2):p2 (x2:xs)
(never mind if there is a better way to do this ...)
p2
is only ever called by p1
. p2
will also never be called with an empty list, only with lists at least one element. However, p2
is still a partial function, and it will trigger a non-exhaustive pattern match warning with ghc -Wall
.
What is the accepted way to deal with this? Do I just not write the p2 []
case? Or p2 [] = undefined
, or p2 [] = unused_value
, or something else?