I am having some difficulty with a program I've written using Haskell. The idea behind it is to recursively find the shortest list in a list of lists and return that. I've managed to write the program okay but I can't seem to figure out what I've done wrong in it. These are the errors that I get when I try to compile it:
- Couldn't match type ‘a’ with ‘[[a]]’, ‘a’ is a rigid type variable bound by the type signature for: shortest :: forall a. [[a]] -> [a] at shortest.hs:1:13. Expected type: [[[a]]], Actual type: [a]
- In the first argument of ‘shortest’, namely ‘y’. In the first argument of ‘(:)’, namely ‘shortest y’. In the expression: shortest y : [list]
- Relevant bindings include list :: [[a]] (bound at shortest.hs:4:15), y :: [a] (bound at shortest.hs:4:13), x :: [a] (bound at shortest.hs:4:11), shortest :: [[a]] -> [a] (bound at shortest.hs:2:1).
Here is the code I'm using:
shortest :: [[a]] -> [a] shortest [] = [] shortest [y] = y shortest (x:y:list) | length x > length y = shortest y:[list] | otherwise = shortest x:[list] If anyone could give me any pointers as to where I'm going wrong it would be much appreciated!
shortest (y:[list])and the same for the x case. The precedence of:makes it read like(shortest y) : [list]