
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?

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

On Wed, Mar 07, 2007 at 04:16:11AM +0000, 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
One nitpick - Hugs isn't a compiler. It is a pure interpreter, and a relatively non-preferred one now, because it is much slower than ghci for small programs, and has relatively less development effort. (eg the development version of ghci has a prototype debugger).
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?
Use an adaptor function: main :: IO () main = do putStr "Enter input file name.txt: " filename <- getLine content <- readFile filename print $ createIndex content createIndex :: String -> [ ([Int], String) ] createIndex = undefined print is a standard library function of type: print :: Show a => a -> IO () That is, it turns any value that can be turned into text, and produces an action. Operationally, it displays the passed-in value on standard output (with a newline).

-----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-----
participants (4)
-
cornmouse
-
Daniel McAllansmith
-
Isaac Dupree
-
Stefan O'Rear