在下面的代碼中,我試圖顯示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感謝你是完全正確的,讓我的左右混合了哈哈。編輯我的帖子。 –