I understand this function and the tail-recursion but I can’t figure out why the strict evaluation is important. Without the strict evaluation it would still be tail-recursive, right? So when will this function fail without strict evaluation?
turboPower a b = turboPower' 1 a b
where
turboPower' x a 0 = x
turboPower' x a b
| x `seq` a `seq` b `seq` False = undefined
| even b = turboPower' x (a*a) (b `div` 2)
| otherwise = turboPower' (x*a) a (b-1)
It will not fail (unless the exponent is huge, so the thunks may become large enough to overflow the stack), it will (may) just be less efficient, since without the strict evaluation, the arguments become thunks, leading to
It shouldn’t be too dramatic here, since the level of nesting is logarithmic in the exponent, and thus remains small for all practical computations, but it would make a huge difference for example in
where the level of nesting is linear in
n.