如何打印两个数字之和的结果?
main:: IO()
main = do putStrLn "Insert the first value: "
one <- getLine
putStrLn "Insert the second value: "
two <- getLine
putStrLn "The result is:"
print (one+two)
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误:
ERROR file:.\IO.hs:3 - Type error in application
*** Expression : putStrLn "The result is:" print (one + two)
*** Term : putStrLn
*** Type : String -> IO ()
*** Does not match : a -> b -> c -> d
Run Code Online (Sandbox Code Playgroud)
Pét*_*zky 10
尝试使用readLn而不是getLine.
getLineString在IOmonad中返回a 并且String无法添加s.
readLn具有多态返回类型,并且编译器推断返回类型是Integer(在IOmonad中),因此您可以添加它们.