Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The base case should describe all lists of length one, the shortest possible non-empty lists:

Code Block

; A non-empty-list-of-numbers (nelon) is one of

...


; - a cons, which has a first, a number, and rest, empty

...


; (cons num empty)

...


; - a cons, which has a first, a number, and rest, a nelon

...


; (cons num nelon)

In this case, the template looks as follows:

Code Block

(define (func-for-nelon a-nelon)

...


    (cond

...


       \[(empty? (rest a-nelon))

...


           …(first a-nelon)…

...

\]
       \[(cons? (rest a-nelon))

...


           …(first a-nelon)…(func-for-nelon (rest a-nelon))…\]))

Solution 2:

We could use the definition of (perhaps-empty) list-of-numbers, that we've already seen. ; A non-empty-list-of-numbers is
; a cons, which has a first, a number, and rest, a list-of-numbers
; (cons num lon)
Here, the template is different, since it must refer to the template for a regular (non-empty) list-of-numbers. In particular, you don't make a recursive call; you think to yourself "Ah, a-nelon contains a (perhaps-empty) list; I'd better call a function for such regular ol' lists": (define (f a-nelon)
…(first a-nelon)…(func-for-lon (rest a-nelon))…)
Here, to compute the average, what function(s) of general (perhaps-empty) lists do you want to call, as your helper?

...