I am developing a function which reads user input of the form “a 2” and then converts it into a tuple and adds it to a list of tuples. This is supposed to keep happening until the user types in “done”.
The code is as follows…
getVectorData vector1 = do
putStrLn "Enter dimension and coefficient separated by a space: Enter \"Done\" to move on to next vector: "
appData <- getLine
if appData == "done" then
putStrLn "That's it"
else do
createVectorTuple (words appData) : vector1
getVectorData vector1
createVectorTuple :: [String] -> (String, Float)
createVectorTuple vectorData = ((head vectorData) , (read (last vectorData) :: Float))
How ever, when trying to execute this file, I get the error
> ERROR file:.\MainApp.hs:13 - Type error in final generator
*** Term : getVectorData vector1
*** Type : IO ()
*** Does not match : [a]
What am I doing wrong?
You are mixing
IOwith pure non-IOfunctions.The above is all
IOcreateVectorTupleis a non-IOfunction. Since the preceding part is anIOdo-block, only expressions of typeIO amay appear in that do-block. However, you get the somewhat strange error message because the precedence of function application is highest, so the above line is parsedwhich is an expression of type
[(String, Float)](ifvector1has that type). Now[]is a monad too, so expressions of type[a]can appear in do-blocks, but then all expressions in that block must have list type. Butis an expression of type
IO ()as has been determined from the part above. Thus the types don’t match. Admittedly, the reported type error is not the clearest possible in that situation.You probably want something along the lines of
or something completely different, I can’t tell from the short snippet.