Haskell,不知道为什么输入'if'*上有*解析错误

Ash*_*dhi 2 haskell functional-programming

这是取一个数字,得到它的阶乘和加倍它,但是因为基本情况如果你输入0它给2作为答案所以为了绕过它我使用了if语句,但是在输入时得到错误解析错误'如果'.真的很感激,如果你们可以帮助:)

fact :: Int -> Int
fact 0 = 1
fact n = n * fact(n-1)

doub :: Int -> Int
doub r = 2 * r

factorialDouble :: IO()
factorialDouble = do 
                    putStr "Enter a Value: "
                    x <- getLine
                    let num = (read x) :: Int
                        if (num == 0) then error "factorial of zero is 0"
                            else let y = doub (fact num) 
                                    putStrLn ("the double of factorial of " ++ x ++ " is " ++ (show y))
Run Code Online (Sandbox Code Playgroud)

Arn*_*non 5

我发现了两个应该解决的问题

  1. 你有一个let没有延续:( else let y = doub (fact num) ...).因为你不在里面do,你可能想把它改成一个let ... in声明.
  2. if缩进太远了.它应该在let.

我已经纠正了我提到的内容,代码对我有用......

fact :: Int -> Int
fact 0 = 1
fact n = n * fact(n-1)

doub :: Int -> Int
doub r = 2 * r

factorialDouble :: IO ()
factorialDouble = do 
                    putStr "Enter a Value: "
                    x <- getLine
                    let num = (read x) :: Int
                    if num == 0 then (error "factorial of zero is 0")
                        else let y = doub (fact num) 
                        in putStrLn ("the double of factorial of " ++ x ++ " is " ++ (show y))
Run Code Online (Sandbox Code Playgroud)