上QQ阅读APP看书,第一时间看更新
Tacit programming with FP
We'll use Haskell to demonstrate a program that sums a list of integers. Both will be recursive, the second benefits from Tail Call Optimization (TCO). We would use Go, but currently Go does not support TCO.
We loop over the list of numbers to accumulate the sum. In imperative programming, we would use a loop index to store the accumulated sum value. In functional programming, we implement loops using recursion where the accumulated sum is passed as a parameter to the next recursive call. What would be loop index variables/accumulator variables in an imperative language become parameters in the tail-recursive version.