[racket] Why does this tail recursive list length function fail at cdr l at end of list?

From: Pierpaolo Bernardi (olopierpa at gmail.com)
Date: Wed Nov 20 06:27:40 EST 2013

On Wed, Nov 20, 2013 at 12:09 PM, Bo Gus <forumangus at gmail.com> wrote:
> My tail recursive implementation of length is like this:
>
> (define (length2 l)
>   (define (l-iter l count)
>     (if (null? 1) count
>         (l-iter (cdr l) (+ count 1))))
>   (l-iter l 0))

> I am fairly new to scheme.  What am I doing wrong?

You wrote (null? 1), which is always false.  Note 1 instead of l.

Cheers

Posted on the users mailing list.