This is a very simple rock paper scissors game implemented in Haskell.
import System.IO (hFlush, stdout)
import System.Random (getStdGen, randomR)
data RPS = Rock | Paper | Scissors deriving (Eq, Show, Read)
instance Ord RPS where
Rock `compare` Scissors = GT
Scissors `compare` Rock = LT
Paper `compare` Rock = GT
Rock `compare` Paper = LT
Scissors `compare` Paper = GT
Paper `compare` Scissors = LT
_ `compare` _ = EQ
prompt :: String -> IO String
prompt text = do
putStr text
hFlush stdout
getLine
main = do
gen <- getStdGen
humanchoice <- prompt "Rock, Paper, or Scissors: "
let (compchoice, _) = randomR (0,2) gen
compchoice' = case (compchoice :: Int) of
0 -> Rock
1 -> Paper
2 -> Scissors
humanchoice' = read humanchoice :: RPS
case compchoice' `compare` humanchoice' of
EQ -> putStrLn "Tie."
GT -> putStrLn "You Lose :("
LT -> putStrLn "You Win! :)"
All kinds of reviews are welcome, but I am specifically interested in knowing what kind of beginner mistakes I might have done. For example, are there things that I could simplify, or are things that you wouldn't see in a intermediate level haskell code?