3 ms·
5.3 is actually not bad code, if it refers to what I think it does. Basically: "Recursive subroutines that concatenate/sum to [...] a carry-along output variab
by lazaroclapp 12y ago
5.3 is actually not bad code, if it refers to what I think it does. Basically:
"Recursive subroutines that concatenate/sum to [...] a carry-along output variable"
Is an accumulator pattern, which is useful in converting certain types of recursive algorithms into tail recursive algorithms with can be handled much better by most compilers (and unrolled into iteration behind the scenes, which means you get clean recursive code that still won't ever run out of stack memory).
- 59nadir 12y agoInitially this was the reason and I still do this, but it should be noted that Racket, for example, will treat the following examples exactly the same and will do just fine with the non-tail recursive example (as far as I can tell): (define (sum/recursive/opt lst [sum 0]) (if (null? lst) sum (sum/recursive/opt (rest lst) (+ sum (first lst))))) (define (sum/recursive/non-opt lst) (if (null? lst) 0 (+ (first lst) (sum/recursive/non-opt (rest lst))))) I don't use any other variants of Scheme with any meaningful frequency, but I still use the uppermost variant because I'm just used to it.