我尝试在 haskell 中创建自己的列表类型,但是我的实现包含错误。这样做的正确方法是什么。请稍微解释一下。谢谢你。
我的代码:
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
错误代码:
[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.