[Haskell-beginners] Help-me read file

Tim Perry perry2of5 at yahoo.com
Wed Oct 27 20:31:11 EDT 2010


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 <gilmarampompelli at gmail.com>
To: beginners at haskell.org
Sent: Mon, October 18, 2010 12:20:21 PM
Subject: [Haskell-beginners] Help-me read file

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
_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://www.haskell.org/mailman/listinfo/beginners



More information about the Beginners mailing list