Non-exhaustive patterns in Haskell function

后端 未结 2 1166
谎友^
谎友^ 2021-01-21 14:08

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)]) ->         


        
2条回答
  •  一个人的身影
    2021-01-21 14:26

    (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.

提交回复
热议问题