I'm trying to write a function in Haskell that will pop some items that have been serialized to a list of strings. (This list represents the lines of a text file -- one line is not necessarily one item)
The function is called with another function as a parameter. This function will pop a single item off, and return a tuple containing that item and the remainder of the text. The function is supposed to recursively perform this operation n times, each time adding the result a list. It returns this list along with the remainder of the text, to be used for further parsing.
popN :: Integer -> [String] -> ([String]-> (a, [String])) -> ([a], [String])
popN n txt fun | n == 0 = ([], txt)
| n /= 0 = do
let (tp, newtxt) = fun txt
let newnum = n - 1
let (rest, after) = popN newnum newtxt fun
return (tp : rest, after)
When I attempt to compile this code, I get the following error:
Couldn't match the expected type '[String]' with actual type '([a], [String])'
In the first argument of 'return', namely '(tp : rest, after)'
The actual type, ([a], [String])
is the type I would expect. What I don't understand, however, is why [String]
is the expected type. Could someone explain to me why GHC expects this function to return a [String]
?
Thanks in advance for any help.