Chapter 2
Basic types and simple programs



Definitions and types

Every definition introduced first has its type specified. This seems pedantic, but, especially in a class or tutorial, it can be justified to students as both a way of understanding a definition presented to them, and also can help get them to see that what they have written as a first attempt needs some modification.


There is obviously a lot of overlap between the basic types of Miranda and those of other programming languages. It is worth stressing the full precision of the integers, as well as the links between record types and tuples.

I have taken the tack of first giving integer functions, and only later identifying that num contains floating point numbers also. This should cause no problems.

Confusion can arise between m, a name; 'm', a character, and "m", a string consisting of a single character. Students should be encouraged to use the Miranda system to tell them the types of expressions, by typing the expression followed by ::.

The sales function

In writing the functions in the early part of the chapter, we need a definition of the sales function. In the code presented, the function's type is given with no definition. For testing purposes it can be defined in two ways: either by a formula (as in Exercise 2.7) or explicitly, thus:

sales 0 = 23
sales 1 = 567 ...
The reason for working relative to this function is to give this early case study a realistic slant; rather than calculating mathematical formulae, the functions analyse data that might have come from the real world.

Booleans as values

A persistent problem is brought to light on page 39: a boolean-valued function has its value assigned by explicitly saying when it has the values True and False. It can be replaced by a simpler definition where the value returned is the boolean expression appearing in the guard. It seems that this point only gets across after the mistake has been made a few times!


The chapter introduces the ideas of top-down and bottom-up design in Section 2.8. The material here can usefully form part of a group discussion about how to build a solution co-operatively.


The chapter contains details about how the simple ideas of calculation can be carried over to cases with where clauses. There are subtleties about the exact order of evaluation of multiple local definitions which are glossed over in the notation. The notation is intended as a tool to assist understanding rather than a fully formal semantics for the language, and should be used in that spirit.

Next Up

Written 18 May 1995.