Haskell pattern matching warning -
Haskell pattern matching warning -
im relatively new haskell (third day learning language) , having problem pattern matching. have defined function doubleeveryother below , far can see have covered 3 possible scenarios: empty list, list of length == 1 , list length > 1. code compiles fine when seek utilize function throw non-exhaustive pattern match error:
*** exception: ex2.hs:(3,1)-(5,55): non-exhaustive patterns in function doubleeveryother i have enabled warnings in ghci, , found next warning when load ex2.hs file:
ex2.hs:3:1: warning: pattern match(es) non-exhaustive in equation `doubleeveryother': patterns not matched: _ : (_ : (_ : _)) line 3:1 refers empty case think have covered doubleeveryother [] = []
i cant see have gone wrong here. help appreciated.
cheers,
-- file: ex2.hs doubleeveryother :: [integer] -> [integer] doubleeveryother [] = [] doubleeveryother (x:[]) = [x] doubleeveryother (_:[xs]) = take (length [xs] - 1) [xs]
the problem in 3rd pattern:
doubleeveryother (_:[xs]) this pattern matches case of list 2 elements (since x:[xs] equivalent [x,xs]). right syntax is:
doubleeveryother (_:xs) haskell
Comments
Post a Comment