Skip to content

Instantly share code, notes, and snippets.

@joom
Last active December 11, 2018 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joom/7d220e603d58ff889902 to your computer and use it in GitHub Desktop.
Save joom/7d220e603d58ff889902 to your computer and use it in GitHub Desktop.
Rock Paper Scissors in Haskell
import System.Random
data Move = Rock | Paper | Scissors deriving (Show, Eq, Enum)
instance Ord Move where
(<=) x y = x == y || (x,y) `elem` [(Rock,Paper),(Paper,Scissors),(Scissors,Rock)]
humanSelect :: IO Move
humanSelect = fmap (toEnum . read) getLine
computerSelect :: IO Move
computerSelect = fmap toEnum (randomRIO (0,2))
resultString :: Ordering -> String
resultString y = case y of
LT -> "Player Wins"
EQ -> "Draw!"
GT -> "Computer Wins"
main :: IO ()
main = do
putStrLn "Choose a move (0 for Rock, 1 for Paper, 2 for Scissors) :"
humanMove <- humanSelect
computerMove <- computerSelect
putStrLn $ show humanMove ++ " (you) vs " ++ show computerMove ++ " (computer)"
putStrLn $ resultString $ humanMove `compare` computerMove
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment