TCO recursive example

In our  tail recursive function, our stack frames do not need to be preserved. 

tSum :: [Integer] -> IntegertSum lst = tSum lst 0 where    tSum (x:xs) i = tSum xs (i+x)    tSum [] i = i

The following diagram illustrates that unlike the previous example (rSum), no action needs to be taken in the context of a frame after tSum makes its recursive call. rSum created a stack frame for each member of the list. tSum only needs to create one stack frame, which it reuses.

TCO avoids creating a new stack frame when the last call in a recursion is the function itself. Go currently does not support TCO. What is the implication? Without ...

Get Learning Functional Programming in Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.