data Tree = Leaf Int | Branch Tree Tree
sumt (Leaf i) acc = i + acc
sumt (Branch l r) acc = sumt l (sumt r acc)
sumt
is in tail position, but it's also in the accumulator. I lean towards no because it has to evaluate the inner call to sumt
(using a call stack), but I can see why it could be tail-recursive because the last function call is sumt
. fibsumt
is clearly tail-recursive even though the acc function creates a deep call-stack.
fib 0 = 0
fib 1 = 1
fib n = fib (n - 2) + fib (n - 1)
fibsumt 0 acc = acc
fibsumt n acc = fibsumt (n - 1) (acc + (fib n))
Is sumt
classified as a tail-recursive function?