Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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.

Code Block

; 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":

Code Block

(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?

...