次のコードでは、階乗の結果を整数で表示しようとしています。私は次のエラーメッセージを受け取りました。私は何が起こったのか、そしてなぜそれが不思議です。ありがとう!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.
ハスケル(関数アプリケーション)は* left * associativeです: 'print factorial2 5'は'(print factorial2)5'と同じです。 –
@DavidYoungありがとうあなたはまったく正しいです、私の左右を笑いあわせました。私の投稿を編集しました。 –