[plt-scheme] Re: Some fine distinctions

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed May 13 08:21:44 EDT 2009

On May 13, 2009, at 7:24 AM, wooks wrote:

>> Didn't fully absorb that. So
>
> sumlists :: listoflists -> number
> (define sumlists
>    (lambda (a-list)
>       (let sumlists ([a-list a-list] [accum 0])
>         (cond
>          [(empty? a-list) 0]
>          [(list? (first a-list)) (sumlists (rest a-list)
>                                   (+ accum (sumlists (first a-list)))]
>          [else (sumlists (rest a-list) (+ accum (first a-list)))]))))
>
> This is accumulative and I used to think ergo tail recursive.
> If I am right and it is not (because of the evaluation of the list?
> case)...is there any way to write this sort of function tail
> recursively. It looks to me not... at least not in this style of
> programming.

Every call to the (inner) sumlist is in tail position, ergo this  
function is tail-recursive.

;; ---

The accumulator statement is

  ;; accum is the sum of all numbers between a-list0 and a-list

(Of course, you failed to name your parameters properly so go back to  
the original reply and check it out.)

;; ---

What I am saying is that accumulator-style does NOT imply tail- 
recursion.



Posted on the users mailing list.