Types


Types

When you write a function definition, you do not know the exact values that somebody will pass to the function. In fact, the same function might be used more than once, and it can be passed different values. For example, a program might at one point ask what occurs('a', "rabbit") is, and at another point ask what occurs('c', "horse") is.

But you are not left completely without information about values. You know what type of values you are given. For example, when you define occurs(c, s), at least you know that c is a character and s is a string.

We have seen a few different types of information: real numbers, integers, boolean values, characters and strings. When you are writing programs it is very important that you think about the type of information that you are working with. It makes sense to subtract two numbers, but not to subtract two characters. You can ask for the length of a string, but not for the length of a real number.

This chapter looks briefly at types, including what happens when you use the wrong type of information. It also looks at how you can think about types and also how you can write information about types in your program, to make your program easier to understand.


Type errors

You know that expression length("wombat") = 6. But what happens if you write length(5.2) in a program?

The answer is that the compiler complains about a type error. It cannot compile what you have written. You will get a lengthy explanation that is not always entirely clear. Try to understand it. If you don't, then look at what you have written to see where you are trying to do something that does not make sense. Ask for help if you cannot see what is wrong.


Writing types in programs

When you define a name, you can indicate the type of thing that the name refers to. Just add : T after the name, where T is the type. Use types Real, Integer, Boolean, Char (for a character) or String. For example,

  Let myName: String = "Larry".
explicitly says that myName is a string. In cases like that where the type is really obvious, there is not much point. But in situations like
  Let y: Real = f(x).
where the answer is computed by some expression, the type can be very helpful.


Types for functions

You can also provide explicit type information with a function definition. Just write what looks like the left-hand side of an equation before the definition, followed by the word by. After each argument name, add a type. Also, after the entire left-hand side, add a type (of the result that the function produces). For example,

  f(x: Real): Real by
  f(x) = 2*x + 1
says that you are defining function f(x) where x is a real number and f(x) produces a real number. The preceding chapter defines function occurs that tests whether a character occurs in a string. With types, its definition is as follows.
  occurs(c: Char, s: String): Boolean by

  case occurs(c, s) = false               when s == ""
  case occurs(c, s) = true                when s_1 == c
  case occurs(c, s) = occurs(c, tail(s))

Be sure to capitalize types. Write Real, not real.


Problems

  1. [solve] Why is type information useful to a programmer?

  2. [solve] Write the following straight-line program with type information added to it. That is, show the type of each name explicitly.

      Let count = 1.
      Let foot = 3.0.
      Let name = "Rose".
    

  3. [solve] Define a function f(x) = x2 - 1, where x is a real number. Add a line showing types.

  4. [solve] Define a function firstChar(s), which produces the first character in string s. Add a line showing types.


Summary

Thinking about types helps you understand what you are doing.

You can write type information in your programs and function definitions. Doing that tends to make programs easier to understand.


Review

Cinnameg and Java both consider integers and real numbers to be different types of things.

A boolean value is either true or false.

A character is what you get when you type one key. Write a character in single quotes, such as 'Q'.

A string is a sequence of zero or more characters. Write a string in double quotes, such as "dragon".