Session 34: I/O

We covered Sections 5 and 6 of Winstanley's article. Hudak's tutorial covers the topic relatively well in Chapter 7 also.

Here is the example we saw in class. The program repeatedly reads lines from the user and displays their reverse.

bellow
wolleb
tips
spit

#!/usr/local/bin/runhugs

module Main where
    readLine :: IO String
    readLine = do c <- getChar
                  if c == '\n'
                   then return ""
                   else do rest <- readLine
                           return (c : rest)

    main :: IO ()
    main = do line <- readLine
              putStrLn (reverse line)
              main

You can run it either within Hugs, or you can execute it directly from the command line:

unix% chmod a+x rev.hs    this makes the file executable
unix% ./rev.hs