[Haskell-beginners] I/O

Daniel Fischer daniel.is.fischer at web.de
Sun Oct 18 09:56:32 EDT 2009


Am Sonntag 18 Oktober 2009 15:42:35 schrieb John Moore:
> Hi,
> Could someone please tell me how to check this program. The problem is
> where do I find the output? I laod this into ghc and it comes up OK modules
> loaded: Main
> gchi>
>
> How do I now check if it works or see the result.
>
> Program below (taken from real world haskell)
> -- file: ch07/toupper-imp.hs
> import System.IO
> import Data.Char(toUpper)
>
> main :: IO ()
> main = do
>        inh <- openFile "quux.txt" ReadMode
>        outh <- openFile "output.txt" WriteMode
>        mainloop inh outh
>        hClose inh
>        hClose outh
> mainloop :: Handle -> Handle -> IO ()
> mainloop inh outh =
>     do ineof <- hIsEOF inh
>        if ineof
>            then return ()
>            else do inpStr <- hGetLine inh
>                    hPutStrLn outh (map toUpper inpStr)
>                    mainloop inh outh
> John

Make sure you have a file quux.txt in your directory, but no valuable file output.txt.
Load the module, type ":main" or just "main" at the ghci prompt and compare the then 
present file output.txt (if it isn't present, that's bad) to quux.txt (open both in an 
editor). It should be the uppercase version of that.

Or compile it (ghc --make toupper-imp.hs) and run the binary.


More information about the Beginners mailing list