Help-me read file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Help-me read file

Gilmara Pompelli
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
Reply | Threaded
Open this post in threaded view
|

Help-me read file

Tim Perry-2
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 <[hidden email]>
To: [hidden email]
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
[hidden email]
http://www.haskell.org/mailman/listinfo/beginners