user1876128
user1876128

Reputation: 91

stack overflow on recursive function haskell

Here is one implementation of power function in Haskell. But I have stack overflow error. No idea how to fix

power''::Integer->Integer->Integer
power'' _ 0=1
power'' 1 _=1
power'' n k
    |even k = power'' (n*n)  (k `div` 2)
    |otherwise = n * power'' n k-1

Upvotes: 0

Views: 372

Answers (2)

user1876128
user1876128

Reputation: 91

--Found my problem

 |otherwise = n * power'' n (k-1)

Upvotes: 0

Ingo
Ingo

Reputation: 36339

|otherwise = n * power'' n k-1

this is

|otherwise = (n * power'' n k)-1

and hence you recurse with the same arguments forever.

Upvotes: 5

Related Questions