【发布时间】:2015-04-18 23:20:13
【问题描述】:
我在让这个功能工作时遇到了很多麻烦:
concatenate :: [String] -> String
它被设计为简单地获取一个字符串列表并返回一个字符串,该字符串是列表中每个元素从头到尾连接的结果。我试图留在map、foldl 和foldr 函数内。我觉得我知道这些函数的概念做得很好,但我遇到的最常见的问题是我遇到了类型冲突。例如,GHC 会期望一个 [Char],我将输入显然试图在我不知道的情况下使用 [[Char]] 的代码。
例如:concatenate (x:xs) = foldr (++) x (concatenate xs)
我得到以下编译错误:
Couldn't match type `Char' with `[Char]'
Expected type: [[Char]]
Actual type: String
In the return type of a call of `concatenate'
In the third argument of `foldr', namely `(concatenate xs)'
In the expression: foldr (++) x (concatenate xs)
我是非常 Haskell 的新手,所以请尽情发笑。只要还包括适合新手的解释,就可以预料到苛刻并受到欢迎。感谢您的所有帮助。
【问题讨论】: