I'm trying to define the Show function for the polymorphic Tree type. Could anyone help me?
import Char
data开发者_如何学Python Tree t =
NilT |
Node t (Tree t) (Tree t)
class Mar t where
maior :: t -> String
instance Mar Tree where
maior (NilT) = "a"
maior (Node t a b) = "b"
instance Show Tree where
show = maior
Thanks a Lot!
Solution (given by ivanm):
import Char
data Tree t =
NilT |
Node t (Tree t) (Tree t)
class Mar t where
maior :: t -> String
instance Mar (Tree t) where
maior (NilT) = "a"
maior (Node t a b) = "b"
instance Show (Tree t) where
show = maior
Is there any particular reason that you aren't using deriving Show
? The Show
and Read
classes are meant to provide really basic serialisation/deserialisation which (usually) produce valid Haskell code.
But for what you want, I think the error is going back to your Mar
class. As defined, the instance is for kind * -> *
(e.g. Maybe
as opposed to Maybe Int
). What you probably meant is to have instance Mar (Tree t) where ...
.
data Tree t =
NilT |
Node t (Tree t) (Tree t)
deriving Show
精彩评论