-- An example (originally of multiple-dispatch), taken from 
-- Functional Programming in Python
-- by David Mertz
-- Copyright Â© 2015 OâReilly Media, Inc.
-- URL: http://www.oreilly.com/programming/free/functional-programming-python.csp

-- For a Pythong version, see RockPaperSciss.py
--
-- Compile: ghc RockPaperSciss.hs
-- Run:     ./RockPaperSciss
-- or
-- Interpreter: ghci RockPaperSciss.hs
--
-----------------------------------------------------------------------------
		   
data Thing = Rock | Paper | Scissors
             deriving (Eq, Read, Show, Ord)

-- verbose version     
beats1 :: Thing -> Thing -> Thing
beats1 Rock Paper     = Paper
beats1 Rock Scissors  = Rock
beats1 Paper Scissors = Scissors
beats1 Paper Rock     = Paper
beats1 Scissors Rock  = Rock
beats1 Scissors Paper = Scissors

-- checks for == and uses symmetry for a more compact version       
beats :: Thing -> Thing -> Thing
beats x y | x==y      = x
          | x<y       = beats' x y     
          | otherwise = beats' y x
                        where     
			  beats' Rock Paper     = Paper
			  beats' Rock Scissors  = Rock
			  beats' Paper Scissors = Scissors
			  beats  _ _            = error "Unknown thingy"

-- to run as a compiled version      
main = putStrLn ("beats " ++ (show x) ++ " " ++ (show y) ++ " = " ++ (show z))
       where x = Paper
             y = Rock
             f = beats
             z = f x y

-- for interpreter version do
-- > ghci ghci RockPaperSciss.hs
-- GHCi> beats Rock Paper
   