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
相关产品推荐
相关产品推荐

