2 ms·
I think you might be confused, because this really doesn't touch on monads at all. You can implement it by just defining an instance of the Num typeclass for y
by dllthomas 12y ago
I think you might be confused, because this really doesn't touch on monads at all. You can implement it by just defining an instance of the Num typeclass for your new data type:
data RPS = Rock | Paper | Scissors
data IRps = IRps Integer RPS
fight Rock Paper = Paper
fight Rock Scissors = Rock
fight Paper Scissors = Scissors
fight Rock Rock = Rock
fight Paper Paper = Paper
fight Scissors Scissors = Scissors
fight x y = fight y x
instance Num IRps where
IRps x x' + IRps y y' = IRps (x + y) (fight x' y')
Of course, with the lack of associativity, I'd weakly discourage a Num instance too, but that's not adhered to perfectly already.