现在请看这个新写的代码,没有使用“读取”,但我仍然在模棱两可的“显示”上得到错误:
data MyType0 a = Tong1 a | Tong2 a deriving Show
data MyType1 a = Cons1 a | Cons2 a | Cons3 | Cons4 deriving Show
data MyType2 a = MyType2 a deriving Show
fun ((Cons2 s):t:ts) (symseq, MyType2 msg) = (symseq, MyType2 (msg ++ ["You provided wrong symbol: " ++ (show t) ++ " Please provide symbol: " ++ (show Cons3) ]))
--fun ((Cons2 s):t:ts) (symseq, MyType2 msg) = (symseq, MyType2 (msg ++ ["You provided wrong symbol: " ++ (show t) ++ " Please provide symbol: " ]))
fun _ syms = syms
ghci 错误消息:
showerr.hs:6:148:
Ambiguous type variable `a0' in the constraint:
(Show a0) arising from a use of `show'
Probable fix: add a type signature that fixes these type variable(s)
In the second argument of `(++)', namely `(show Cons3)'
In the second argument of `(++)', namely
`" Please provide symbol: " ++ (show Cons3)'
In the second argument of `(++)', namely
`(show t) ++ " Please provide symbol: " ++ (show Cons3)'
Failed, modules loaded: none.
请注意,注释部分不会给出此错误。请解释为什么这是错误的。
原始消息保留在下面。
我在一个网站上得到了以下代码。我知道“读取”功能可能会出现模棱两可的类型错误,但在这里我也可以为“显示”功能找到它。我觉得很奇怪,不明白。
代码:
main = do run <- getLine
val <- getLine
case run of
"len" -> print . show . len $ (read val)
"rev" -> print . show . rev $ (read val)
_ -> putStr "wrong option"
rev :: [a] -> [a]
rev = foldl (flip (:)) []
len :: [a] -> Int
len = foldl (\ac _ -> ac + 1) 0
当我在 ghci 中加载它时出现错误。
ideone_x0cMx.hs:4:46:
Ambiguous type variable `a0' in the constraint:
(Read a0) arising from a use of `read'
Probable fix: add a type signature that fixes these type variable(s)
In the second argument of `($)', namely `(read val)'
In the expression: print . show . len $ (read val)
In a case alternative: "len" -> print . show . len $ (read val)
ideone_x0cMx.hs:5:46:
Ambiguous type variable `a1' in the constraints:
(Read a1) arising from a use of `read' at ideone_x0cMx.hs:5:46-49
(Show a1) arising from a use of `show' at ideone_x0cMx.hs:5:32-35
Probable fix: add a type signature that fixes these type variable(s)
In the second argument of `($)', namely `(read val)'
In the expression: print . show . rev $ (read val)
In a case alternative: "rev" -> print . show . rev $ (read val)
Failed, modules loaded: none.