how to convert IO String to string (Simple answer)

David Sankel camio@yahoo.com
Sun, 24 Nov 2002 18:15:23 -0800 (PST)


--- Lu Mudong <mudong@hotmail.com> wrote:
> Thanks a lot for you guys' help.
> 
> I am very new to haskell and tried some methods you
> guys advised, doesn't 
> seem to work, i think i didn't do it properly,
> here's my code and result, 
> hope you can point out what's wrong. thanks!
>

Lots of theory, here's some code:

-- Begin code
module Main()
where

import IO

myReadFile :: String -> IO String
myReadFile filename = readFile filename 

main :: IO ()
main = do
  s <- myReadFile "/etc/fstab"
  let length = doStuffWithNormalString s
  putStr "Length of File :" 
  putStr (show length)
  putStr "\n"


-- For example, count the number of characters
doStuffWithNormalString :: String -> Int
doStuffWithNormalString s = length s;

-- End code

My Explination:
Basically, your program starts in a do loop.  Anything
that returns an IO something needs a <-.  For example

a <- someIOReturnValue

And you can use a as a normal value w/o an IO type.

Anything function that doesn't return the IO type
needs a 

let a = someNormalFunction.

And you can pretty much do what you want after you
know these things.  Oh yea, you can't do IO stuff in a
non IO function.  This is a pretty nasty part of
learning haskell, but once you get used to it, you
might actually like it.

Later,

David J. Sankel