pow功能的无限循环

Laz*_*ert 0 haskell function operator-precedence

我是Haskell的新手,我在这里得到了一个无限循环,但我不知道为什么.

module Main where

pow :: Int -> Int -> Int
pow x 0 = 1
pow x y = x * pow x y-1

main :: IO ()
main = print( pow 2 3 )
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Rak*_*111 8

pow x y = x * pow x y-1
Run Code Online (Sandbox Code Playgroud)

不会做你认为它做的事情.它被解析为

pow x y = (x) * (pow x y) - (1)
                ^^^^^^^^^
               infinite loop
Run Code Online (Sandbox Code Playgroud)

现在,您可以更清楚地看到无限循环.你需要括号y-1,

pow x y = x * pow x (y-1)
Run Code Online (Sandbox Code Playgroud)