plt /7/ Terminology aggregate: contains values of other types, e.g., array or record with named fields. union: values are of other types. discriminated union: value contains explanatory tag and value of union's types. variant record: discriminated union of record types. Scheme values are discriminated union of primitive types. Inductively defined data types can be represented as variant records.
plt /7/ define-datatype Scheme extension (define-datatype type-name type-predicate-name (variant-name (field-name predicate) … ) … ) creates variant record type with one or more variants with zero or more fields type and variant names must be globally unique must be used at top level
plt /7/ define-datatype (2) creates type-predicate-name predicate with one argument creates variant-name constructor with one argument per field (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?)))
plt /7/ Sum of leaves of bintree (define leaf-sum (lambda (tree) (cases bintree tree (leaf-node (datum) datum) (interior-node (key left right) (+ (leaf-sum left) (leaf-sum right)) ) ) (leaf-sum (interior-node 'foo (leaf-node 1) (leaf-node 2) )
plt /7/ cases (cases type-name expression (variant-name (field-name …) body) … (else body) ) value of expression must be of type-name variant selects appropriate variant-name each field value is bound to field-name and body is executed without else, all variants must be specified