List algorithm in interactive tutorial gives error

For the Compleat Fan
Locked
bluenote
Posts: 1
Joined: Sat Nov 29, 2014 1:42 am

List algorithm in interactive tutorial gives error

Post by bluenote »

Hi There,

The generic list algorithm in the interactive tutorialproduces an error when I try to evaluate it.

I was able to fix up the code and get it working (provided below) as per the spirit of the tutorial. Perhaps that tutorial is out of date to newer language features or something.


Old non-working code

Code: Select all

(define (list-length a-list)
(if (first a-list)
  (+ 1 (list-length (rest a-list)))
  0))
My modified version which seems to work

Code: Select all

(define (list-length a-list)
(if a-list
  (+ 1 (list-length (rest a-list)))
  0))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: List algorithm in interactive tutorial gives error

Post by Lutz »

Thanks for the correction - now online. The change should have been made in 2008 when the empty list would would now be taken as Boolean false by if and first on an empty list returned now an error.

Locked