Home > other >  Haskell - A problem with non-exhaustive patterns in function
Haskell - A problem with non-exhaustive patterns in function

Time:10-12

I have the following haskell code:

f :: [[(a,[b])]] -> Int
f [(x,xs):[y,ys]] = 0

x0 :: [[(Int,[Int])]]
x0 = [   [(1,[1]),(1,[1])]   ,   [(1,[1]),(1,[1])]   ]

Why doesn't x0's pattern matching to function f?

CodePudding user response:

y and ys are two elements of the list. This thus means that the pattern:

(x,xs) : [y, ys] is short for (x, xs) : y : ys : []. The list thus contains three items: as first item a 2-tuple (x, xs) as second item y and as third item ys.

You can thus match this with:

--      ↓      ↓        ↓ three items in the sublist
[   [(1,[1]),(1,[1]), (1,[1])]]

or you should match this with:

f :: [[(a, [b])]] -> Int
f (((x, xs) : _) : y : _) = 0
  • Related