I am learning to use typeclasses in haskell.
The following code works.
data Mood = Blah | Woot deriving (Show)
changeMood :: Mood -> Mood
changeMood Blah = Woot
changeMood Woot = Blah
main = do
print (changeMood Blah)
print (changeMood Woot)
But the following gives error error: Not in scope: data constructor ‘Mood’
.
data Mood = Blah | Woot
changeMood :: Mood -> Mood
changeMood Blah = Woot
changeMood Woot = Blah
instance Show Mood where
show(Mood x) = "Mood: " show(x)
main = do
print (changeMood Blah)
print (changeMood Woot)
How can I make the second code work (implement show
function).
CodePudding user response:
Mood
is a type, not a value. It can be used only in type signatures, after ::
, and in constraints (as in instance Show Mood
).
The values are Blah
and Woot
, so use those:
instance Show Mood where
show Blah = "Mood: Blah"
show Woot = "Mood: Woot"