0

Here is the first one, which is attempting to take two lists and print all the differences between the two .

EX.

> list_diff [1,6,1,3,4,3,5] [3,8,5,4,7,4,8] [1,6,1,8,7,8]

list_diff :: Eq a => [a] -> [a] -> [a]
list_diff [] [] = []
list_diff x y = list_helper x y

list_helper [] [] = []
list_helper (x:xs) y
     | x `elem` y = list_helper xs y
     | otherwise = x:(list_helper xs y)

When I run a test for this one (using the example provided above), I reviece the following output:

[1,6,1*** Exception: HW1.hs:(12,1)-(15,39): Non-exhaustive patterns in function list_helper

For the second funtion, I am attempting to take a series of values and determine which list is the longest

EX.

progLanguages = 
     [ ("CptS121" , ["C"]), 
     ("CptS122" , ["C++"]), 
     ("CptS223" , ["C++"]), 
     ("CptS233" , ["Java"]), 
     ("CptS321" , ["C#"]), 
     ("CptS322" , ["Python", "JavaScript"])]

INPUT: max_count progLanguages

OUTPUT: ("CptS322" , 2)

What I have so far isn't working at all, so needless to say I am stuck.

max_count [] = error "bad"
max_count [x] = x
max_count (x:xs) = x max_helper (max_count xs)
     where
     max_helper (a,b) (a',b')
          | length b > length b' = (a, length b)
          | otherwise = (a', length b')

Thanks in advance for any help you can give.

J.Bauman
  • 79
  • 5

1 Answers1

2

You've defined list_helper:

list_helper [] [] = []
list_helper (x:xs) y
     | x `elem` y = list_helper xs y
     | otherwise = x:(list_helper xs y)

The warning about non-exhaustive pattern matching means that your function is evaluating arguments that you haven't told it how to handle. In this case, that's the first argument being an empty list, but the second argument not being empty.

You need to handle this pattern:

list_helper [] [] = []
list_helper [] y = y
list_helper (x:xs) y
     | x `elem` y = list_helper xs y
     | otherwise = x:(list_helper xs y)
Prelude> :{
Prelude| list_helper [] [] = []
Prelude| list_helper [] y = y
Prelude| list_helper (x:xs) y
Prelude|      | x `elem` y = list_helper xs y
Prelude|      | otherwise = x:(list_helper xs y)
Prelude| :}
Prelude> list_helper [1, 2, 3] [4, 5, 6]
[1,2,3,4,5,6]
Chris
  • 26,361
  • 5
  • 21
  • 42