Haskell:错误:无法将类型“ []”与“ IO”匹配

时间:2018-10-11 11:21:07

标签: haskell

我正在编写一个简单的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的新手。如何解决此错误?谢谢。

1 个答案:

答案 0 :(得分:9)

addExFunction inputString。在主do块中,您需要使用IO操作。您想对字符串做什么?如果要打印,请说:

main = do
   ...
   putStrLn (addExFunction input)