[Haskell-cafe] Haskell File reading

Isaac Dupree isaacdupree at charter.net
Wed Mar 7 06:26:29 EST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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?

If you're going to be doing something more with the value later in the
'do' block, you can use 'let' in the do block (since createIndex doesn't
(have to) do any IO -- there is no IO in its type -- it doesn't have to
be run as an IO action (in fact it can't be, which is why there is an
error message)) :

do
       putStr "Enter input file name.txt: "
       filename <- getLine
       content <- readFile filename
       let contentIndex = createIndex content
       ... ... probably use contentIndex somewhere ... ...

Isaac
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF7qFkHgcxvIWYTTURApwoAKDIn0TcRgD+TRePt0FSxvNph+cyqgCeKhQd
yEvF/zEO+eYDivMivXhOmOE=
=mcFN
-----END PGP SIGNATURE-----


More information about the Haskell-Cafe mailing list