COM1340 - Winter 2003 Recursive types January 8, 2003 |
struct mytree { int x; struct mytree *left; struct mytree *right; };
In this type, why are the self-similar pieces pointers to structures, rather than structures, here? What sizes of trees can we denote using this type?
This C/C++ structure might have arisen from a slightly more mathematical definition:
A mytree is eitherNote the recursion in the definition -- that recursion corresponds directly to the recursion in the struct type above. In this definition, why do we need the first alternative?
NULL, or
a triple containing an integer and left and right mytrees
An extremely useful recursive type is the list. Just as lists are useful in everyday life (shopping lists, phone lists, to-do lists), they're handy in programs. Lucky for us, the list is a native data structure in Scheme.
Let's define the type of data in Scheme, which includes lists.
A Scheme value is eitherAtoms are constants, such as numbers, strings, and symbols:
an atom, or
a list
42 3.14159 "the quick brown fox" 'truth-and-beauty 'a