Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function pow函数中的无限循环_Function_Haskell_Operator Precedence - Fatal编程技术网

Function pow函数中的无限循环

Function pow函数中的无限循环,function,haskell,operator-precedence,Function,Haskell,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 ) 有什么想法吗 pow x y = x * pow x y-1 不会做你认为它会做的事。它被解析为 pow x y = (x) * (pow x y) - (1)

我是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 )
有什么想法吗

pow x y = x * pow x y-1
不会做你认为它会做的事。它被解析为

pow x y = (x) * (pow x y) - (1)
                ^^^^^^^^^
               infinite loop
现在您可以更清楚地看到无限循环。您需要在y-1中插入

pow x y = x * pow x (y-1)

x+pow x y-1
的意思是
(x)+(pow x y)-(1)
,而不是
x+(pow x(y-1))
。谢谢,这是有意义的=)当然不是(+),但是(*)=)@Lazyexpert是的,但这不是问题:)