一个haskell" exp"函数返回错误的结果

时间:2015-10-07 13:30:11

标签: haskell

以下代码:

Module Main where

main :: IO ()
main = do putStrLn "hello"
          putStrLn $ "2 exp 6 = " ++ show (2 `exp1` 6)

exp1 :: Integer -> Integer -> Integer
exp1 x n | n == 0 = 1
         | n == 1 = x
         | even n =     exp1 (x*x) m
         | odd  n = x * exp1 (x*x) (m-1)
         where m = n `div` 2

4生成输出2 `exp1` 6,这显然是错误的。

感谢

1 个答案:

答案 0 :(得分:6)

奇怪的情况是错误的。您最终将exp1 4 3评估为4 * (exp1 16 0)