在下面的代码中,我试图显示factorial(整数)的结果。我收到以下错误消息,我想知道发生了什么以及为什么。谢谢!在if-else语句中无法显示/打印int
factorial2 0 = 1
factorial2 n = n * factorial2 (n-1)
main = do putStrLn "What is 5! ?"
x <- readLn
if x == factorial2 5
then putStrLn "Right"
-- else print factorial2 5 -- why can't pass here
-- else show factorial2 5 -- why can't pass here
else putStrLn "Wrong" -- this can pass, no problem
-- Factorial.hs:10:20:
-- Couldn't match expected type ‘Integer -> IO()’
-- with actual type ‘IO()’
-- The function ‘print’ is applied to two arguments,
-- but its type ‘(a0 -> a0) -> IO()’ has only one
-- In the expression: print factorial2 5
-- In a stmt of a 'do' block:
-- if x == factorial2 5 then putStrLn "Right" else print factorial2 5
-- Failed, modules loaded: none.
我会说Haskell(函数应用程序)是* left * associative:'print factorial2 5'与'(print factorial2)5'相同。 –
@DavidYoung感谢你是完全正确的,让我的左右混合了哈哈。编辑我的帖子。 –