Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-exhaustive patterns in Haskell function

Tags:

list

haskell

I have a problem with a haskell program. I want to do something like this:

main = do
    print $ map foo [(1, [(2, 3), (4,5)])]

foo :: (Int, [(Int, Int)]) -> (Int, [(Int, Int)])
foo (a, [(b, c)]) = (a+1, [(b, c)])

Then i get the run-time error:

Non-exhaustive patterns in function Main.foo

How is it possible to make such a action? I just want to access the parameters which are not in the list.

like image 829
develhevel Avatar asked Nov 20 '25 08:11

develhevel


1 Answers

(a, [(b, c)]) does not match (1, [(2, 3), (4, 5)]), because the list in the latter has two elements while your pattern requires there to be only one.

If you want to leave the list unchanged, use this pattern instead:

foo (a, bar) = (a+1, bar)

Now bar will match [(2, 3), (4, 5)] because it is just a binding which will match anything of the correct type.

like image 189
hammar Avatar answered Nov 22 '25 02:11

hammar