If our program requires anything more than atomic data, we must define
new datatypes. In Typed Scheme, structures can be defined, similarly
to PLT Scheme structures. The following program defines a date
structure and a function that formats a date as a string, using PLT
Scheme's built-in format function.

Here we see the new built-in type str as well as a definition
of the new user-defined type my-date. To define
my-date, we provide all the information usually found in a
define-struct, but added type annotations to the fields.
Then we can use the functions that this declaration creates, just as
we would have with define-struct.

In this module, we have defined two new datatypes: leaf and
node. We've also used the type (Unodeleaf),
which represents a binary tree of numbers. In essence, we are saying
that the tree-height function accepts either a node
or a leaf and produces a number.

In order to calculate interesting facts about trees, we have to take
them apart and get at their contents. But since accessors such as
node-left require a node as input, not a
(Unodeleaf), we have to determine which kind of input we
were passed.

For this purpose, we use the predicates that come with each defined
structure. For example, the leaf? predicate distinguishes
leafs from all other Typed Scheme values. Therefore, in the
first branch of the cond clause in tree-sum, we know
that t is a leaf, and therefore we can get its value
with the leaf-val function.

In the else clauses of both functions, we know that t is not
a leaf, and since the type of t was (Unodeleaf) by process of elimination we can determine that t must
be a node. Therefore, we can use accessors such as
node-left and node-right with t as input.

When a complex type is used repeatedly in a program, it can be helpful
to give it a short name. In Typed Scheme, this can be done with type aliases. For example, we could have used a type alias to
represent our tree datatype from the previous section.

The defintion (define-type-aliastree (Unodeleaf)) creates
the type alias tree, which is just another name for the type
(Unodeleaf). We can then use this type in subsequent
definitions such as tree-sum.

1 Changes
to uses of require may also be necessary -- these are
described later.