How to avoid stack overflow in Haskell?

前端 未结 1 1975
隐瞒了意图╮
隐瞒了意图╮ 2021-01-07 22:21

Haskell does not support cycling for computation, instead it offers to use recursion algorithms. But this approach leads to growing of stack, and even stack overflow. I beli

相关标签:
1条回答
  • 2021-01-07 22:50

    The problem here is not the recursion, but the laziness of your n argument. Stack overflows in Haskell come from trying to evaluate deeply-nested thunks; in your case, each call to doWork initTime (n+1) is making a slightly-deeperly-nested thunk in the second argument. Simply make it strict, and things should be happy again: doWork initTime $! n+1.

    0 讨论(0)
提交回复
热议问题