[plt-scheme] Some fine distinctions

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Wed May 13 08:38:41 EDT 2009

On Wednesday, May 13, 2009, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
>
> 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.

And, just in case this was not already clear: tail-recursion does not
imply accumulator either. For example a function that returns the last
element of a (non-empty) list will likely be tail-recursive but won't
have an accumulator.

Robby


> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.