
Hello I have a file with 100 lists, with 100 ints. I have to read the file and apply the map and sort functions on lists. II did it to read file: learquivo :: FilePath -> IO ([[Int]]) learquivo s = do conteudo <- readFile s return (read conteudo) But now applying the sort, the error appears: <interactive>:1:5: Couldn't match expected type `[a]' against inferred type `IO [[Int]]' how can I fix? Thanks

Hi Gilmara,
I assume you are trying to do this:
main = do
map sort $ learquivo "dataFile.txt"
which gives you the error you mention. The problem is you are trying to use a
"IO [[Int]]" where you want a "[[Int]]". To data out of IO, you must use the
"<-" operator. To write a program which reads a line from the user and echos it
back you do this:
main = do
userLine <- readLine
putStrLn userLine
The type of userLine is "String" (or "[Char]" if you prefer).
Similarly, to change your "IO [[Int]]" to an [[Int]] you need to use the <-
operator.
So your program could become:
main = do
intListOfLists <- learquivo "dataFile.txt"
let sortedSubLists = map Data.List.sort intListOfLists
.....
Does this help? If not, read the "True Nature of Return" section of Real World
Haskell: http://book.realworldhaskell.org/read/io.html#io.return
Hope this helps,
Tim
----- Original Message ----
From: Gilmara Pompelli
participants (2)
-
Gilmara Pompelli
-
Tim Perry