Comp 210 Lab 3: List definition

Using lists is so common that Scheme has list functions built-in, except using some different names from those used when we introduced them. (Labbies: Note that students have seen both versions.)

Example List Definition
  In-class introduction For use from now on
Data definition
; A list-of-symbols is one of
;   - empty
;   - (make-los f r)
; where f is a symbol,
; and r is a list-of-symbols.
; A list-of-symbols is one of
;   - empty
;   - (cons f r)
; where f is a symbol,
; and r is a list-of-symbols.
(On paper, we'd also draw an arrow highlighting the self-reference of "list-of-symbols".)
(define-struct los (f r)) No define-struct needed.
Constructors empty
make-los
empty
cons
empty is technically a constructor, even though it is a constant.
Selectors los-f
los-r
first
rest
Predicates empty?
los?
empty?
cons?
Thus, we will really use the same constructors, selectors, and predicates for lists of symbols, lists of numbers, or lists of whatever else.

(Although students saw the shorthand (list 'a 'short 'example) in class, we'll strongly emphasize the above recursive definition for now.)