1

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)
6
  • 2
    What values did you call the function with? Commented Mar 16, 2019 at 10:14
  • And what actually happened? Your title says stack overflow, but your actual question mentions an endless loop (presumably a non-responses program). These are different things and have different causes. Commented Mar 16, 2019 at 10:31
  • it shows C-stack overflow , so i guess the problem is in recursion Commented Mar 16, 2019 at 10:32
  • 1
    Incidentally, you don't need the n `mod` d == 0 check. If something's divisible by d^3 then it's automatically divisible by d Commented Mar 16, 2019 at 10:33
  • Stack overflows in Haskell are not caused by recursion itself, as they are in imperative languages. Haskell stack overflows are due to too much laziness. Commented Mar 16, 2019 at 10:35

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.