I'm new to Haskell, and I'm getting an infinite loop here, but I don't know why.
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 )
Any ideas?
pow x y = x * pow x y-1
doesn't do what you think it does. It is parsed as
pow x y = (x) * (pow x y) - (1)
^^^^^^^^^
infinite loop
Now you can see the infinite loop more clearly. You need to parenthesize y-1
,
pow x y = x * pow x (y-1)