New instance declaration for Show

后端 未结 3 965
我寻月下人不归
我寻月下人不归 2021-02-01 14:50

I\'m trying to add an instance declaration in Haskell for a new data type I\'ve created unsuccessfully. Here what I\'ve tried so far:

data Prediction = Predictio         


        
3条回答
  •  醉酒成梦
    2021-02-01 15:17

    You've got the syntax for instances wrong. To create an instance of Show write:

    instance Show Foo where
      show = ...
      -- or
      show x = ...
    

    where ... contains your definition of the show function for Foo.

    So in this case you want:

    instance Show Prediction where
      show = showPrediction
    

    or, since there isn't an important reason to have showPrediction at all:

    instance Show Prediction where
      show (Prediction a b c) = show a ++ "-" ++ show b ++ "-" ++ show c
    

提交回复
热议问题