You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Haskell中如何对比IO String与String,不匹配时打印消息并返回值?

求等效Python代码的Haskell实现

我想实现一段与以下Python代码等效的Haskell代码:

def inputThing(string: str) -> bool:
  a=input("Input: ")
  if a=="Hello":
    return True
  else:
    print("Not hello")
    return False

我的尝试

我自己写出了如下Haskell代码:

import System.IO ( hFlush, stdout )
inputThing :: String -> IO Bool
inputThing string = do
  putStr "Input:"
  hFlush stdout
  input <- getLine
  if string == input then True else putStrLn "Not hello" >> False

之后按照代码检查工具提示简化最后一行:

(string == input) || (putStrLn "Not hello" >> False)

但出现了多个IO类型相关错误:

thing.hs:7:3: error:
    • Couldn't match expected type ‘IO Bool’ with actual type ‘Bool’
    • In a stmt of a 'do' block:
        (string == input) || (putStrLn "Not hello" >> False)
      In the expression:
        do putStr "Input:"
           hFlush stdout
           input <- getLine
           (string == input) || (putStrLn "Not hello" >> False)
      In an equation for ‘inputThing’:
          inputThing string
            = do putStr "Input:"
                 hFlush stdout
                 input <- getLine
                 ....
  |
7 |   (string == input) || (putStrLn "Not hello" >> False)
  |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

thing.hs:7:25: error:
    • Couldn't match expected type ‘Bool’ with actual type ‘IO b0’
    • In the second argument of ‘(||)’, namely
        ‘(putStrLn "Not hello" >> False)’
      In a stmt of a 'do' block:
        (string == input) || (putStrLn "Not hello" >> False)
      In the expression:
        do putStr "Input:"
           hFlush stdout
           input <- getLine
           (string == input) || (putStrLn "Not hello" >> False)
  |
7 |   (string == input) || (putStrLn "Not hello" >> False)
  |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

thing.hs:7:49: error:
    • Couldn't match expected type ‘IO b0’ with actual type ‘Bool’
    • In the second argument of ‘(>>)’, namely ‘False’
      In the second argument of ‘(||)’, namely
        ‘(putStrLn "Not hello" >> False)’
      In a stmt of a 'do' block:
        (string == input) || (putStrLn "Not hello" >> False)
  |
7 |   (string == input) || (putStrLn "Not hello" >> False)
  |                                                 ^^^^^

我尝试改写为:

(fmap ((==) string) input) || (putStrLn "Not hello" >> return False)

但仍然报错:

thing.hs:7:3: error:
    • Couldn't match expected type ‘IO Bool’ with actual type ‘Bool’
    • In a stmt of a 'do' block:
        (fmap ((==) string) input)
          || (putStrLn "Not hello" >> return False)
      In the expression:
        do putStr "Input:"
           hFlush stdout
           input <- getLine
           (fmap ((==) string) input)
             || (putStrLn "Not hello" >> return False)
      In an equation for ‘inputThing’:
          inputThing string
            = do putStr "Input:"
                 hFlush stdout
                 input <- getLine
                 ....
  |
7 |   (fmap ((==) string) input) || (putStrLn "Not hello" >> return False)
  |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

thing.hs:7:4: error:
    • Couldn't match expected type ‘Bool’ with actual type ‘[Bool]’
    • In the first argument of ‘(||)’, namely
        ‘(fmap ((==) string) input)’
      In a stmt of a 'do' block:
        (fmap ((==) string) input)
          || (putStrLn "Not hello" >> return False)
      In the expression:
        do putStr "Input:"
           hFlush stdout
           input <- getLine
           (fmap ((==) string) input)
             || (putStrLn "Not hello" >> return False)
  |
7 |   (fmap ((==) string) input) || (putStrLn "Not hello" >> return False)
  |    ^^^^^^^^^^^^^^^^^^^^^^^^

thing.hs:7:23: error:
    • Couldn't match type ‘Char’ with ‘[Char]’
      Expected: [String]
        Actual: String
    • In the second argument of ‘fmap’, namely ‘input’
      In the first argument of ‘(||)’, namely
        ‘(fmap ((==) string) input)’
      In a stmt of a 'do' block:
        (fmap ((==) string) input)
          || (putStrLn "Not hello" >> return False)
      In the expression:
        do putStr "Input:"
           hFlush stdout
           input <- getLine
           (fmap ((==) string) input)
             || (putStrLn "Not hello" >> return False)
  |
7 |   (fmap ((==) string) input) || (putStrLn "Not hello" >> return False)
  |                       ^^^^^

thing.hs:7:34: error:
    • Couldn't match expected type ‘Bool’ with actual type ‘IO Bool’
    • In the second argument of ‘(||)’, namely
        ‘(putStrLn "Not hello" >> return False)’
      In a stmt of a 'do' block:
        (fmap ((==) string) input)
          || (putStrLn "Not hello" >> return False)
      In the expression:
        do putStr "Input:"
           hFlush stdout
           input <- getLine
           (fmap ((==) string) input)
             || (putStrLn "Not hello" >> return False)
  |
7 |   (fmap ((==) string) input) || (putStrLn "Not hello" >> return False)
  |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

求助

我清楚无法将IO String转换为String,但不知道如何得到一个能返回IO String与String对比结果的IO Bool,尝试多次后仍未成功,希望得到正确的实现方式。


内容的提问来源于stack exchange,提问作者Electroboss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:57:07