Hangman.hs
by
管理者
—
last modified
2011-12-27 08:48
Click here to get the file
Size
1.3 kB
-
File type
text/x-haskell
File contents
-- http://www.cs.nott.ac.uk/~gmh/errata.html $B$N(B Eratta
-- Page 90. The action getCh is no longer provided as a primitive in Hugs,
-- but can be defined in Haskell as follows:
import System.IO
getCh :: IO Char
getCh = do hSetEcho stdin False
c <- getChar
hSetEcho stdin True
return c
-- This definition works with the Glasgow Haskell Compiler,
-- but may not work with some Haskell systems, such as Hugs.
-- $B0J2<$O%9%i%$%I$NDj5ADL$j(B
hangman :: IO ()
hangman = do putStrLn "Think of word: "
word <- sgetLine
putStrLn "Try to guess it:"
guess word
sgetLine :: IO String
sgetLine = do x <- getCh
if x == '\n' then
do putChar x
return []
else
do putChar '-'
xs <- sgetLine
return (x:xs)
guess :: String -> IO ()
guess word = do putStr "> "
xs <- getLine
if xs == word then
putStrLn "You got it!"
else
do putStrLn (diff word xs)
guess word
diff :: String -> String -> String
diff xs ys = [if elem x ys then x else '-' | x <- xs]