package main /** * A tail recursive and not tail recursive function in Go * Since the Go compiler does not implement tail call optimization, tail recursion * is irrelevant * Created: Sep 20, 2023 * gtowell **/ //a function that is NOT tail recursive // just compute the sum of some numbers // this will stack overflow func notTail(p int64) int64 { return p + notTail(p+1) } // internal tail recursive implementation of summer // Still stack overflow since GO does not do tail call optimization // just calls the TR version of summing, adding the extra param required for // tail recursion. func tail(p int64) int64 { var iTail func(int64, int64) int64 // internal tail recursive implementation of summer // Still stack overflow since GO does not do tail call optimization iTail = func(pp int64, aa int64) int64 { if pp>10000000 { return aa } return iTail(pp+1, aa+pp) } return iTail(p, 0) } func main() { //notTail(0) println(tail(0)) }