Comp 210 Lab 4: And More Data Structures

Index: S-expressions, Variations on natural numbers


S-Expressions

Many kinds of software needs to be able to represent a program as a piece of data. For example, to DrScheme, your programs are just pieces of data which it needs to understand how to evaluate. Such software needs a data definition of the programming language that is to be represented. Will will represent a subset of Scheme code as data, using the following definitions.

A S-expression is one of

and an atom is one of

The third case of S-expressions allows us to represent the clauses of a conditional. It also allows us to represent more general Scheme expressions that you'll see later in the course.

Let's make some example S-expressions:

S-expression represented Scheme code
(list '+ 3 2)( (+ 3 2)
(list 'cons 19 'empty) (cons 19 empty)
(list 'cond (list #t 'x)) (cond (#t x))
We can also see that there are some Scheme expressions which cannot be represented using these S-expressions:
     3
     fred
     (symbol? 'hello)
(Note that we could have defined a type to represent any Scheme expression, but the given definition is simpler to use.)

To do: Develop the function atom? : any -> boolean.

To do: Develop the template for functions of type

     f : S-expression -> X
Follow the data definition! You'll want to use atom?.

To do: Using this template, write the following functions. Where applicable, finish the examples.


Variations on natural numbers

We have defined that a NatNum is either

If we wanted to start counting from some other number, say, 1 or 15 or -77, we could change our definition slightly. E.g., a NatNum[>=10] is either

To do: Make some example data for this type and write the appropriate template.

If we wanted to count downwards instead, we could again change our definition. E.g., a NatNum[<=10] is either

To do: Make some example data for this type and write the appropriate template.

Rather than forcing yourself to use some standard well-known data definition, it is often important to use your own variant which matches your needs more precisely. But, when you do so, it is extra important to be clear that you are not using the standard definition, so the reader is not confused.