[Haskell-cafe] Haskell File reading

Daniel McAllansmith dm.maillists at gmail.com
Tue Mar 6 23:46:32 EST 2007


On Wednesday 07 March 2007 17:16, cornmouse wrote:
> I have a txt file, which contains a paragraph. I am trying to read the
> file, and pass the contents of the file as a string to another function
> called "createIndex". "createIndex" is a function to generate index of the
> input string.
>
> Below is my code to read the file :
>
> main :: IO ()
> main
>      = do
>          putStr "Enter input file name.txt: "
>          filename <- getLine
>          content <- readFile filename
>          createIndex content
>
> createIndex  ::  String  ->  [ ([Int], String) ]
> createIndex
>        {- contents of the function... -}
>
> I am using Hugs compiler, when i execute, i got an error
> Type error in generator
> *** Term           : showString content
> *** Type           : [Char] -> [Char]
> *** Does not match : IO a
>
> IO>
>
> I know the data type doesn't match. How do i go about this?

What do you want to do with the index?

Maybe you want to print the index out, in which case change
createIndex content
to
putStrLn (show (createIndex content))

That'll work because

show :: (Show a) => a -> String

turns your index into a string, and then

putStrLn :: String -> IO ()

takes the string and carries out an IO computation, that has a side effect of 
putting some text on screen, and returns a value of (), which matches the 
type of main.

Daniel


More information about the Haskell-Cafe mailing list