簡體   English   中英

Haskell:錯誤:無法將類型“ []”與“ IO”匹配

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM