我做:
Prelude> "sone" ++ "otehr"
"soneotehr"
Run Code Online (Sandbox Code Playgroud)
但是这样的代码:
addOneToElement :: [a] -> [a]
addOneToElement element = element ++ "next"
main = do
let s = addOneToElement("some")
putStrLn s
Run Code Online (Sandbox Code Playgroud)
产生这个输出:
all_possible_combinations.hs:22:37:
Couldn't match expected type `a' against inferred type `Char'
`a' is a rigid type variable bound by
the type signature for `addOneToElement'
at all_possible_combinations.hs:21:20
Expected type: [a]
Inferred type: [Char]
In the second argument of `(++)', namely `"next"'
In the expression: element ++ "next"
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误以及如何解决?
您的类型签名应该是:
addOneToElement :: [Char] -> [Char]
Run Code Online (Sandbox Code Playgroud)
(或者更简单地说,addOneToElement :: String -> String)
a类型签名中的" "是一个通配符 - 它可以匹配任何内容.但是,您尝试将列表连接Char到任何列表 - 并且没有办法做到这一点.