The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

449049 (17) [Avatar] Offline
Q14.2 asks that we do the following:

Define a five-sided die (FiveSidedDie type). Then define a type class named Die and at least one method that would be useful to have for a die. Also include superclasses you think make sense for a die. Finally, make your FiveSidedDie an instance of Die.

The answer is:

data FiveSidedDie = Side1 | Side2 | Side3 | Side4 | Side5 deriving (Enum, Eq, Show)

class (Eq a, Enum a) => Die a where
  roll :: Int -> a
instance Die FiveSidedDie where
  roll n = toEnum (n `mod` 5)

Since we are saying that FiveSidedDie is an instance of our Die type class, wouldn't it be redundant for the FiveSidedDie type to be deriving Eq and Enum again?

Also, (this might be silly) I'm having trouble calling the `roll` function. If I call it as follows:

roll 5

I get the following error:

    No instance for (Die a0) arising from a use of ‘roll’
    The type variable ‘a0’ is ambiguous
    Relevant bindings include b :: a0 (bound at ch14-exercises.hs:49:1)
    Note: there is a potential instance available:
      instance Die FiveSidedDie -- Defined at ch14-exercises.hs:45:10
    In the expression: roll 5
    In an equation for ‘b’: b = roll 5
Failed, modules loaded: none.