Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issues with two Haskell commands, one attempting to find and print differences in two lists, and one attempting to output the longest length

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Yu 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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading