I try to create an own list type in haskell but, my implementation contains errors.
What is the proper way to do this nice. Please explain me a bit.
Thank you.
My code:
data List a = EmptyList | ListElement a (List a)
instance (Show a) => Show (List a) where
show = showList'
showList' EmptyList = showString "[]"
showList' (ListElement a EmptyList) = show a
showList' (ListElement a b) = show a ++ show " " ++ showList' b
Error code:
[1 of 1] Compiling Main ( tipusok.hs, interpreted )
tipusok.hs:12:39:
Couldn't match expected type `Prelude.String -> Prelude.String'
with actual type `[Char]'
Expected type: ShowS
Actual type: Prelude.String
In the return type of a call of `show'
In the expression: show a
Failed, modules loaded: none.
The type of
showStringisString -> ShowS.ShowSis a type synonym forString -> String, so the result ofshowString "[]"is the function that prepends the string"[]"to its argument. Since you gave no type signature, that equation determines what type is inferred for the function, but the other equations don’t match that type.You probably wanted simply