I was trying to get the list with the greatest sum within a list and then return that list. But when I call the function with
max_list [[1,2],[3,6],[10,34,5]]
it gives me the error:
Exception: a4.hs:65:1-64: Non-exhaustive patterns in function max_list
This is the code:
max_num :: [Int] -> Int
max_num [x] = x
max_num (x:xs) | (max_num xs) > x = maxVal xs
| otherwise = x
max_list :: [[Int]] -> [Int]
max_list [[a]] = head(filter (\x -> (sum_int x) == (max_num [[a]]) [[a]])
My logic is as follows: I will
- Sum the elements in the sublist
- Compare that element to see if it equals the max-value of the list
- Filter out the values that do not equal the max-value
Example call:
head (filter (\x -> (sum x) == 11) [[1,3],[4,7],[2,5]])
> [4,7]
So in that case I calculated the value 11 before hand and its sum of each element is [4, 11, 7] and it will give me the value whose sum is equal to the max value