Comp 210 Lab 4: Heterogeneous Lists

Heterogeneous lists are those that can contain different kinds of data, e.g., symbols or numbers.

Solution 1: Use the definition of symbol-or-number.

; A list of symbols or numbers (lo-sym+num) is either
;   - empty
;   - (cons f r)
;     where f is a symbol-or-number, r is a lo-sym+num

Solution 2: Have one case for each kind of item.

; A list of symbols or numbers (lo-sym+num) is either
;   - empty
;   - (cons f r)
;     where f is a symbol, r is a lo-sym+num
;   - (cons f r)
;     where f is a number, r is a lo-sym+num

When you develop programs, you should use the template corresponding to the definition you choose.

  1.      (define (f losn)
            (cond
               [(empty? losn) ...]
               [else          ...(first losn)...(f (rest losn))]))
         
    where f probably calls another function on (first losn) that consumes a symbol-or-number.

  2.      (define (f losn)
            (cons
               [(empty? losn)          ...]
               [(symbol? (first losn)) ...(first losn)...(f (rest losn))...]
               [(number? (first losn)) ...(first losn)...(f (rest losn))...]))
         
    The answers of the two last cases typically, but not necessarily, have the same recursive calls.

The first choice is usually the better option.