我正在编写一个简单的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的新手。如何解决此错误?谢谢。
答案 0 :(得分:9)
addExFunction input
是String
。在主do
块中,您需要使用IO操作。您想对字符串做什么?如果要打印,请说:
main = do
...
putStrLn (addExFunction input)