include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/should_run.mk
-HC_OPTS += -dcore-lint
+HC_OPTS += -dcore-lint -syslib posix -syslib misc -fglasgow-exts
+io022_HC_OPTS += -fglasgow-exts
+io010_HC_OPTS += -fglasgow-exts
+io011_HC_OPTS += -fglasgow-exts
-io002_RUNTEST_OPTS = -x 1
+io004_RUNTEST_OPTS += -x 42
+io016_RUNTEST_OPTS += io016.hs io016.out
+io017_RUNTEST_OPTS += -i io017.stdin
+io021_RUNTEST_OPTS += -i io021.hs
+io022_RUNTEST_OPTS += -i io022.hs
+
+.PRECIOUS: %.o %.bin
include $(TOP)/mk/target.mk
import System (getEnv)
-main =
- getEnv "TERM" >>= \ term ->
- putStr "Got $TERM" >>
- putChar '\n' >>
- getEnv "One fish, two fish, red fish, blue fish" >>= \ fish ->
- putStr fish >>
- putChar '\n'
+import IO ( isDoesNotExistError )
+
+main :: IO ()
+main = do
+ term <- getEnv "TERM"
+ putStrLn "Got $TERM"
+ fish <- getEnv "One fish, two fish, red fish, blue fish" `catch` getEnv_except
+ putStrLn fish
+
+getEnv_except :: IOError -> IO String
+getEnv_except ioe
+ | isDoesNotExistError ioe = return ""
+ | otherwise = fail ioe
-io003 with some arguments
+io003.bin
case ec of
ExitSuccess -> putStr "What?!?\n" >> fail (userError "dog succeeded")
ExitFailure _ ->
- system "cat Main.hs 2>/dev/null" >>= \ ec ->
+ system "cat io005.hs 2>/dev/null" >>= \ ec ->
case ec of
ExitSuccess -> exitWith ExitSuccess
ExitFailure _ -> putStr "What?!?\n" >> fail (userError "cat failed")
case ec of
ExitSuccess -> putStr "What?!?\n" >> fail (userError "dog succeeded")
ExitFailure _ ->
- system "cat Main.hs 2>/dev/null" >>= \ ec ->
+ system "cat io005.hs 2>/dev/null" >>= \ ec ->
case ec of
ExitSuccess -> exitWith ExitSuccess
ExitFailure _ -> putStr "What?!?\n" >> fail (userError "cat failed")
import IO -- 1.3
-main =
- hClose stderr >>
- hPutStr stderr "junk" `catch` \ err -> if isIllegalOperation err then putStr "Okay\n" else error "Not okay\n"
-
+main = do
+ hClose stderr
+ hPutStr stderr "junk" `catch` \ err -> if isIllegalOperation err then putStr "Okay\n" else error "Not okay\n"
import IO -- 1.3
main =
- openFile "io007.in" ReadMode >>= \ hIn ->
+ openFile "io007.hs" ReadMode >>= \ hIn ->
hPutStr hIn "test" `catch`
\ err ->
if isIllegalOperation err then
+import IO -- 1.3
+
main =
- openFile "io007.in" ReadMode >>= \ hIn ->
- hPutStr hIn "test" `handle`
- \ (IllegalOperation _) ->
+ openFile "io007.hs" ReadMode >>= \ hIn ->
+ hPutStr hIn "test" `catch`
+ \ err ->
+ if isIllegalOperation err then
hGetContents hIn >>= \ stuff ->
hPutStr stdout stuff
+ else
+ error "Oh dear\n"
import Directory (removeFile)
-main =
- openFile "io008.in" ReadMode >>= \ hIn ->
- openFile "io008.out" ReadWriteMode >>= \ hOut ->
- removeFile "io008.out" >>
- hGetPosn hIn >>= \ bof ->
- copy hIn hOut >>
- hSetPosn bof >>
- copy hIn hOut >>
- hSeek hOut AbsoluteSeek 0 >>
- hGetContents hOut >>= \ stuff ->
- putStr stuff
+main = do
+ hIn <- openFile "io008.in" ReadMode
+ hOut <- openFile "io008.out" ReadWriteMode
+ removeFile "io008.out"
+ bof <- hGetPosn hIn
+ copy hIn hOut
+ hSetPosn bof
+ copy hIn hOut
+ hSeek hOut AbsoluteSeek 0
+ stuff <- hGetContents hOut
+ putStr stuff
copy :: Handle -> Handle -> IO ()
copy hIn hOut =
try (hGetChar hIn) >>=
- either (\ err -> if isEOFError err then return () else error "copy") ( \ x -> hPutChar hOut x >> copy hIn hOut)
+ either (\ err -> if isEOFError err then return () else error "copy")
+ ( \ x -> hPutChar hOut x >> copy hIn hOut)
+
import Directory (getDirectoryContents)
-import QSort (sort)
+import List (sort, isPrefixOf)
-main =
- getDirectoryContents "." >>= \ names ->
- print (sort names)
+main = do
+ names <- getDirectoryContents "."
+ let names' = filter (isPrefixOf "io009") names
+ putStrLn (unlines (sort names'))
-[".", "..", ".depend", "CVS", "Main.hi", "Main.hs", "Main.o", "Makefile", "io009", "io009.stdout"]
+io009.bin
+io009.hs
+io009.o
+io009.stdout
+
import Directory (getCurrentDirectory, setCurrentDirectory,
createDirectory, removeDirectory, getDirectoryContents)
-main =
- getCurrentDirectory >>= \ oldpwd ->
- createDirectory "foo" >>
- setCurrentDirectory "foo" >>
- getDirectoryContents "." >>= \ [n1, n2] ->
- if dot n1 && dot n2 then
- setCurrentDirectory oldpwd >>
- removeDirectory "foo" >>
+main = do
+ oldpwd <- getCurrentDirectory
+ createDirectory "foo"
+ setCurrentDirectory "foo"
+ ~[n1, n2] <- getDirectoryContents "."
+ if dot n1 && dot n2
+ then do
+ setCurrentDirectory oldpwd
+ removeDirectory "foo"
putStr "Okay\n"
- else
+ else
fail (userError "Oops")
-
dot :: String -> Bool
dot "." = True
dot ".." = True
import Directory
import IOExts (trace)
-main =
- createDirectory "foo" >>
- openFile "foo/bar" WriteMode >>= \ h ->
- hPutStr h "Okay\n" >>
- hClose h >>
- renameFile "foo/bar" "foo/baz" >>
- renameDirectory "foo" "bar" >>
- openFile "bar/baz" ReadMode >>= \ h ->
- hGetContents h >>= \ stuff ->
- putStr stuff >>
- hClose h >>
- removeFile "bar/baz" >>
+main = do
+ createDirectory "foo"
+ h <- openFile "foo/bar" WriteMode
+ hPutStr h "Okay\n"
+ hClose h
+ renameFile "foo/bar" "foo/baz"
+ renameDirectory "foo" "bar"
+ h <- openFile "bar/baz" ReadMode
+ stuff <- hGetContents h
+ putStr stuff
+-- hClose h -- an error !
+ removeFile "bar/baz"
removeDirectory "bar"
import CPUTime
-main =
- openFile "/dev/null" WriteMode >>= \ h ->
- hPrint h (nfib 30) >>
- getCPUTime >>= \ t ->
- print t
+main = do
+ h <- openFile "/dev/null" WriteMode
+ hPrint h (nfib 30)
+ t <- getCPUTime
+ print (length (show t)) -- printing the CPU time itself is un-cool if you want to diff the output..
nfib :: Integer -> Integer
nfib n
import IO -- 1.3
-main =
- openFile "io013.in" ReadMode >>= \ h ->
- hFileSize h >>= \ sz ->
- print sz >>
- hSeek h SeekFromEnd (-3) >>
- hGetChar h >>= \ x ->
- putStr (x:"\n") >>
- hSeek h RelativeSeek (-2) >>
- hGetChar h >>= \ w ->
- putStr (w:"\n") >>
- hIsSeekable h >>= \ True ->
- hClose h >>
- openFile "/dev/null" ReadMode >>= \ h ->
- hIsSeekable h >>= \ False ->
+main = do
+ h <- openFile "io013.in" ReadMode
+ sz <- hFileSize h
+ print sz
+ hSeek h SeekFromEnd (-3)
+ x <- hGetChar h
+ putStr (x:"\n")
+ hSeek h RelativeSeek (-2)
+ w <- hGetChar h
+ putStr (w:"\n")
+ ~True <- hIsSeekable h
hClose h
-
+ h <- openFile "/dev/null" ReadMode
+ ~False <- hIsSeekable h
+ hClose h
+
-[True, True, True]
-[False, False, False]
-[True, False, False]
-[False, True, True]
-[True, True, False]
-[False, False, False]
-[False, False, True]
+[True,True,True]
+[False,False,False]
+[True,False,False]
+[False,True,True]
+[True,True,False]
+[False,False,False]
+[False,False,True]
-import IO -- 1.3
-
-main =
- isEOF >>= \ eof ->
- if eof then
- return ()
- else
- getChar >>= \ c ->
- putChar c >>
- main
import System (getArgs)
import Char (toUpper)
+import Directory (removeFile)
main = getArgs >>= \ [f1,f2] ->
openFile f1 ReadMode >>= \ h1 ->
openFile f2 WriteMode >>= \ h2 ->
+ removeFile f2 >>
copyFile h1 h2 >>
hClose h1 >>
hClose h2
-- It simply hangs on stdin!
import IO -- 1.3
+import Directory (removeFile)
-import System(getArgs)
-
-main = getArgs >>= \ [user,host] ->
- let username = (user ++ "@" ++ host) in
+main = let username = "io018.inout" in
openFile username ReadWriteMode >>= \ cd ->
+ removeFile username >>
hSetBuffering stdin NoBuffering >>
hSetBuffering stdout NoBuffering >>
hSetBuffering cd NoBuffering >>
speakString = "Someone wants to speak with you\n"
-speak cd =
+speak cd = return ()
+{-
(hReady cd >>= \ ready ->
if ready then (hGetChar cd >>= putChar)
else return () >>
else return ()) >>
speak cd
+-}
+
import Time
main =
- getClockTime >>= \ time ->
- print time >>
+ getClockTime >>= \ time ->
+ print (length (show time)) >>
let (CalendarTime year month mday hour min sec psec
wday yday timezone gmtoff isdst) = toUTCTime time
- in
- putStr (wdays !! fromEnum wday) >>
- putStr (' ' : months !! fromEnum month) >>
- putStr (' ' : shows2 mday (' ' : shows2 hour (':' : shows2 min (':' : shows2 sec
- (' ' : timezone ++ ' ' : shows year "\n")))))
+ time2 = wdays !! fromEnum wday ++
+ (' ' : months !! fromEnum month) ++
+ (' ' : shows2 mday (' ' : shows2 hour (':' : shows2 min (':' : shows2 sec
+ (' ' : timezone ++ ' ' : shows year "\n")))))
+ in
+ print (length time2)
where
wdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
time' = toClockTime (CalendarTime (year - 1) month mday hour min sec psec
wday yday timezone gmtoff isdst)
in
- print time >>
+ print (length (show time)) >>
putChar '\n' >>
- print time' >>
+ print (length (show time')) >>
putChar '\n'
+import IO -- 1.3
+
+main =
+ hSetBuffering stdin NoBuffering >>
+ hSetBuffering stdout NoBuffering >>
+ interact id
--- /dev/null
+module Main(main) where
+
+import IO
+import IOExts
+import Char
+
+main = do
+ isT <- hIsTerminalDevice stdin
+ flg <- if not isT then return False else hGetEcho stdin
+ print flg
+ if not isT then hSetEcho stdin False else return ()
+ hSetBuffering stdin NoBuffering
+ interact (map toUpper)
+
--- /dev/null
+False
+MODULE MAIN(MAIN) WHERE
+
+IMPORT IO
+IMPORT IOEXTS
+IMPORT CHAR
+
+MAIN = DO
+ IST <- HISTERMINALDEVICE STDIN
+ FLG <- IF NOT IST THEN RETURN FALSE ELSE HGETECHO STDIN
+ PRINT FLG
+ IF NOT IST THEN HSETECHO STDIN FALSE ELSE RETURN ()
+ HSETBUFFERING STDIN NOBUFFERING
+ INTERACT (MAP TOUPPER)
+
--- /dev/null
+--!!! Testing output on stdout
+module Main(main) where
+
+-- stdout is buffered, so test if its buffer
+-- is flushed upon program termination.
+main :: IO ()
+main = putStr "Hello"
--- /dev/null
+Hello
\ No newline at end of file