AngD
AngD

Reputation: 11

Why we have a stack overflow in a recursive function in haskell?

where do i have an endless loop?

 f2 :: Int->Int->Int
 f2 n d 
    | d==2
       = 0 
    | n `mod` d ==0  && n`mod` d^3==0
       = 1 + (f2 n d-1)
    | otherwise
       = 0 + (f2 n d-1)

Upvotes: 1

Views: 82

Answers (1)

sepp2k
sepp2k

Reputation: 370162

This is a precedence issue. (f2 n d-1) is parsed as (f2 n d) - 1, which leads to infinite recursion because f2 is calling itself with the same arguments. You want f2 n (d - 1) instead.

Upvotes: 9

Related Questions