【发布时间】:2019-03-05 23:04:30
【问题描述】:
我正在玩弄自定义列表显示。这个概念非常简单,但我不断收到 IO() 错误。我的代码是:
displayList :: [Int] -> IO()
displayList [] = putStrLn ""
displayList (firstUnit:theRest) = putStrLn (show firstUnit ++ "\n" ++
displayList theRest)
我得到的错误代码是:
• Couldn't match expected type ‘[Char]’ with actual type ‘IO ()’
• In the second argument of ‘(++)’, namely ‘(displayList theRest)’
In the first argument of ‘putStrLn’, namely
‘((show firstUnit) ++ (displayList theRest))’
In the expression:
putStrLn ((show firstUnit) ++ (displayList theRest))
出现错误的行的特定部分是 displayList theRest 而不是 putStrLn ((show firstUnit) ++ 部分。
我想我理解正在发生的事情,即当displayList theRest 在错误行中被调用时,它最终有可能在几次递归调用后从displayList [] = putStrLn "" 行返回一个 IO() 类型,即不支持作为 putStrLn 函数中的输入。有人知道解决这个问题的方法吗?
【问题讨论】:
-
displayList在任何情况下都不会返回字符串。它总是返回IO,所以... ++ displayList theRest没有意义。你最好有这个函数构造并返回一个字符串,然后根据需要打印字符串。 -
问题是你试图将
String(或[Char],意思相同)与displayList的结果连接起来,即IO ()- 它是很明显你不能这样做。我不确定您希望如何输出,但您可能想调用putStrLn (show firstUnit),然后在theRest上递归调用displayList。 “then”是指对 2 个 IO 操作进行排序,可以在do块中或使用>>运算符(相同,这是do块转换为的内容)。 -
在任何编程语言(包括命令式语言)中都会遇到同样的问题。您需要弄清楚您是否希望
displayList构建并返回一个字符串,或者它是否应该输出内容(并且什么都不返回)。 -
你目前的情况就像是在说
printf("%d\n%s", firstUnit, printf(theRest))...