[英]Haskell: Error: Couldn't match type ‘[]’ with ‘IO’
我正在编写一个简单的Haskell程序,其中添加了感叹号! 到用户输入的字符串的末尾。 但是我的程序无法运行。 这是我的代码:
addExFunction :: String -> String
addExFunction x = x ++ "!"
main = do
putStrLn "enter string: "
input <- getLine
addExFunction input
这是我的错误:
a.hs:7:2: error:
• Couldn't match type ‘[]’ with ‘IO’
Expected type: IO Char
Actual type: String
• In a stmt of a 'do' block: addExFunction input
In the expression:
do putStrLn "enter string: "
input <- getLine
addExFunction input
In an equation for ‘main’:
main
= do putStrLn "enter string: "
input <- getLine
addExFunction input
|
7 | addExFunction input
| ^^^^^^^^^^^^^^^^^^^
我是Haskell的新手。 如何解决此错误? 谢谢。
addExFunction input
是一个String
。 在主要的do
块中,您需要改为使用IO操作。 您想对字符串做什么? 如果要打印,请这样说:
main = do
...
putStrLn (addExFunction input)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.