Haskell pattern matching warning

Dave0504

im relatively new to Haskell (third day learning the language) and am having a problem with pattern matching. I have defined the function doubleEveryOther below and as far as I can see I have covered the three possible scenarios: empty list, list of length == 1 and list length > 1. The code compiles fine but when a try to use the function it throw a non-exhaustive pattern match error:

*** Exception: ex2.hs:(3,1)-(5,55): Non-exhaustive patterns in function doubleEveryOther

I have then enabled warnings in GHCI, and found the following warning when I load the ex2.hs file:

ex2.hs:3:1: Warning:
    Pattern match(es) are non-exhaustive
    In an equation for `doubleEveryOther':
        Patterns not matched: _ : (_ : (_ : _))

Line 3:1 refers to the empty case which I think I have covered with doubleEveryOther [] = []

I cant see where I have gone wrong here. Help appreciated.

Cheers,

-- file: ex2.hs
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther [] = []
doubleEveryOther (x:[]) = [x]
doubleEveryOther (_:[xs]) = take (length [xs] - 1) [xs]
Benesh

The problem is in the third pattern:

doubleEveryOther (_:[xs])

This pattern matches the case of a list with two elements (since x:[xs] is equivalent to [x,xs]). The correct syntax is:

doubleEveryOther (_:xs)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related