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

From: Bo Gus (forumangus at gmail.com)
Date: Wed Nov 20 06:33:18 EST 2013

I noticed that just after posting.

I changed to:
(define (length2 lst)
  (define (l-iter l count)
    (if (null? l) count
        (l-iter (cdr l) (+ count 1))))
  (l-iter lst 0))

Must get better at spotting obvious mistakes...


On 20 November 2013 11:27, Pierpaolo Bernardi <olopierpa at gmail.com> wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131120/c78c2103/attachment.html>

Posted on the users mailing list.