Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"non-Exhaustive patterns in function listelem" haskell

Tags:

haskell

Hi i'm very new to haskell. I'm trying to write a code that will allow me to enter a list of coordinate systems (CS1) (i.e a list of list of coordinates) and a list of all coordinates (CL) that are not allowed. The aim of the function is to discard all coordinate systems in CS1 that contain atleast 1 of these coordinates in CL, ending up with a smaller subset of coordinate systems(CS2) from CS1. (confusing? i'm sorry)

I think i'm quite close to crack the nut (although i really don't know as i'm in the trail error stage), but i'm having a non-Exhaustive problem when i run listelem2 it complains on listelem. Can anyone see what i'm missing? Thanks for any help you can give! :D

listelem0 :: (Eq a) => [a] -> a -> [a]
listelem0 [] y = []
listelem0 (x:xs) y 
            | x == y = []
            | x /= y = [x] ++ listelem0 xs y

listelem :: (Eq a) => [a] -> a -> [a]
listelem (x:xs) y
      | length (listelem0 (x:xs) y) < length (x:xs) = []
      | otherwise = listelem0 (x:xs) y

listelem1 :: (Eq a) => [[a]] -> a -> [[a]]
listelem1 [] y = []
listelem1 (x:xs) y = [listelem x y] ++ listelem1 xs y  

listelem2 :: (Eq a) => [[a]] -> [a] -> [[a]]
listelem2 [] [] = [[]]
listelem2 (x:xs) [] = (x:xs)
listelem2 (x:xs) (y:ys) = listelem2 (listelem1 (x:xs) y) ys
like image 647
Stayhannebo Avatar asked Oct 27 '13 20:10

Stayhannebo


2 Answers

Emil is right, that you're missing those 2 patterns, but here's how you can find those missing patterns yourself:

If you run ghci -Wall yourFile.hs (or ghci -fwarn-incomplete-patterns yourFile.hs), you'll see all the incomplete patterns:

[1 of 1] Compiling Main             ( /tmp/ls.hs, interpreted )

/tmp/ls.hs:2:1:
    Warning: Pattern match(es) are non-exhaustive
             In an equation for `listelem0': Patterns not matched: (_ : _) _

/tmp/ls.hs:8:1:
    Warning: Pattern match(es) are non-exhaustive
             In an equation for `listelem': Patterns not matched: [] _

/tmp/ls.hs:17:1:
    Warning: Pattern match(es) are non-exhaustive
             In an equation for `listelem2': Patterns not matched: [] (_ : _)
Ok, modules loaded: Main.

Let's take the last one for example: In an equation for listelem2': Patterns not matched: [] (_ : _) -- this means just what it sounds like: that the pattern listelem2 [] (something:somethingElse) is never matched.

like image 145
amindfv Avatar answered Sep 24 '22 03:09

amindfv


You have no patterns for these calls:

  • listelem [] y
  • listelem2 [] (y:ys)
like image 30
Emil Vikström Avatar answered Sep 25 '22 03:09

Emil Vikström