I'm trying to implement show method of a data type.
data OptionList a b = EmptyOpt | OptionList { optListHead :: a, optListTail :: b } deriving (Read)
instance (Show a, Show b) => Show (OptionList a b) where
show (OptionList a EmptyOpt) = "{" ++ (show a) ++"}"
show (OptionList EmptyOpt b) = "{" ++ (show b) ++"}"
show (OptionList a b) = "{"++ (show a) ++ ", " ++ (show b) ++"}"
show EmptyOpt = ""
I want the OptionList not to show a comma if one of a or b has a value constructed by EmptyOpt. But the compiler shows the following error:
OptionList.hs:11:28:
Couldn't match expected type ‘b’
with actual type ‘OptionList t0 t1’
‘b’ is a rigid type variable bound by
the instance declaration at OptionList.hs:10:10
Relevant bindings include
show :: OptionList a b -> String (bound at OptionList.hs:11:9)
In the pattern: EmptyOpt
In the pattern: OptionList a EmptyOpt
In an equation for ‘show’:
show (OptionList a EmptyOpt) = "{" ++ (show a) ++ "}"
OptionList.hs:12:26:
Couldn't match expected type ‘a’
with actual type ‘OptionList t2 t3’
‘a’ is a rigid type variable bound by
the instance declaration at OptionList.hs:10:10
Relevant bindings include
show :: OptionList a b -> String (bound at OptionList.hs:11:9)
In the pattern: EmptyOpt
In the pattern: OptionList EmptyOpt b
In an equation for ‘show’:
show (OptionList EmptyOpt b) = "{" ++ (show b) ++ "}"
UPDATE: OptionList is supposed to be something like a typeless list.
(+:) :: a -> b -> (OptionList a b)
infixr 5 +:
t1 +: t2 = OptionList t1 t2
So, a list like: 0 +: "test" +: True would be defined like OptionList Int (OptionList String (OptionList Bool EmptyOpt)) And would be shown as {0, {"test", {True}}}