简体   繁体   English

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

[英]Haskell: Error: Couldn't match type ‘[]’ with ‘IO’

I'm writing a simple Haskell program that adds an exclamation mark ! 我正在编写一个简单的Haskell程序,其中添加了感叹号! to the end of an inputted string by the user. 到用户输入的字符串的末尾。 However my program won't run. 但是我的程序无法运行。 This is my code: 这是我的代码:

addExFunction :: String -> String
addExFunction x = x ++ "!"

main = do
 putStrLn "enter string: "
 input <- getLine
 addExFunction input

This is what my error looks like: 这是我的错误:

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
  |  ^^^^^^^^^^^^^^^^^^^

I am new to Haskell. 我是Haskell的新手。 How do I fix this error? 如何解决此错误? Thank you. 谢谢。

addExFunction input is a String . addExFunction input是一个String In the main do block, you need to use IO actions instead. 在主要的do块中,您需要改为使用IO操作。 What do you want to do with the string? 您想对字符串做什么? If you want to print it, say so: 如果要打印,请这样说:

main = do
   ...
   putStrLn (addExFunction input)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM