0

I am pretty new to Haskell and am having trouble with this function.

The function gets an Int and a list of tuples and has to return Bool if the Int exists in the list.

When I try to compile it , it doesn't return any errors , but when I try to test it, I get Non-exhaustive patterns in function kommt_vor

--help func to turn list of tuples into a list
tupleToList :: [(Int,Int)] -> [Int]
tupleToList ((a,b):xs) = a : b : tupleToList xs
tupleToList _          = []

kommt_vor :: Int -> [(Int,Int)] -> Bool
kommt_vor xs [(a,b)] = xs `elem` tupleToList[(a,b)]
kommt_vor xs [] = False

Any help would be great

General Grievance
  • 4,555
  • 31
  • 31
  • 45
miwa_p
  • 435
  • 1
  • 4
  • 19
  • 1
    If you add `{-# OPTIONS_GHC -Wall #-}` to the top of the file, it will give you a warning that tells you which cases you are missing in your pattern match. – Hjulle Oct 22 '20 at 20:28
  • 2
    The `[(a,b)]` pattern will only match with lists with *exactly one* element. – Willem Van Onsem Oct 22 '20 at 20:38
  • ``xs `elem` tupleToList[(a,b)]`` is equivalent to `xs == (a,b)` -- not what you want. You need to handle lists with more than one pair. Even better, don't make it more complex than needed, don't pattern match anything. Use something like `kommt_vor i xs = ...` – chi Oct 22 '20 at 20:44
  • @chi not sure I quite understand what you mean with **kommt_vor i xs = ...** I thought if I use the **tupleToLists** on elem than it would work as if I passed only a list of values ...sorry total nobie to haskell – miwa_p Oct 22 '20 at 20:55
  • @AmilaBečirović: but the problem is that you pattern match with `kommt_vor xs [(a,b)]` and this will only fire if the list contains one 2-tuple. So then it is "too late". – Willem Van Onsem Oct 22 '20 at 20:57
  • @WillemVanOnsem should I be doing the **kommt_vor** recursively then ? – miwa_p Oct 22 '20 at 21:19
  • 1
    @AmilaBečirović: no, just use a variable, not a pattern, so ``kommt_vor xs ys = xs `elem` tupleToList ys``. – Willem Van Onsem Oct 22 '20 at 21:20
  • @WillemVanOnsem thanks, I just figured why I got it wrong before. I actually first started similar like you but instead of simple **ys** I added **[ys]** and that didn't work. again thanks for the help ! – miwa_p Oct 22 '20 at 21:25
  • @AmilaBečirović: wel `[ys]` is a pattern with a singleton list, and that element is `ys`. – Willem Van Onsem Oct 22 '20 at 21:26
  • @WillemVanOnsem makes sense ...couldn't figure it out before, somehow ..still trying to learn how it all works – miwa_p Oct 22 '20 at 21:30

0 Answers0