Haskell Problem

Lars Lundgren d95lars@dtek.chalmers.se
Wed, 11 Oct 2000 11:04:31 +0200 (MEST)


On Tue, 10 Oct 2000, Graeme Turner wrote:

> Hello,
> 
> I am e-mailing you to see if you could offer me a bit of assistance. I have
> chosen to use
> Haskell in a minor assignment at my University, Heriot Watt in Edinburgh.
> The basic aim is to read in a file of data, sort it and then display it.
> 
> I have managed to get a sort to function properly but I am having trouble
> with reading in the data from the file. I have managed to use the
> hGetContents and hGetLine methods of the IO library to read the data in but
> when it is read in, it is stored as an IO String type.
> 
> I would like to convert the input from the file into one large string so I
> can process it before sorting it.
> 

Aha, you want to use readFile :: String -> IO String.

> After reading the whole file into a variable, how do I then convert that IO
> String to a String?
> 

You use '<-' in the do notation.

readFile "myfile.txt"   -- here you have an IO String

do contents <- readFile "myfile.txt"
   return (lines contents)

-- here you "convert" the IO String (readFile "myfile.txt") to a String
-- (contents). Finally my example returns a list of the lines in the file.

I hope this helps.
/Lars L