next up previous contents
Next: Sets Up: Exploring the Basics Previous: Variables

   
Simple types

We touched upon some simple types in Section [*] where we saw examples of variables of type real, integer, posint, boolean and string. Types have the following pragmatic advantages:



Table: Some basic types in Darwin. See Table [*] for a complete list of all built-in types.
1.1 
Table: Some basic types in Darwin. See Table [*] for a complete list of all built-in types.
Type Description Expression
real Real numbers $-1, -1.01, 99.9, \ldots$
integer Integer numbers $\ldots, -1, 0, 1, 2, \ldots$
posint Integers greater than zero $1, 2, \ldots$
boolean Boolean values true, false
string A sequence of symbols 'hello', '12345'
  surrounded by single quotes  
name A legal Darwin variable, abc, x where
  procedure or structure name x:= proc() end;
constant Any real value 1, 3.583, -11
  or variable assigned a value  
 



All data has a type associated with it and to test the type of an item of data there is a built-in Darwin function
type(exp, tn)
where exp is any expression and tn is any Darwin type. This function returns true if the expression expevaluates to type tn or false otherwise.

For instance,

> type(5, real);                 # real numbers are integers and 
true
> type(1.3, real);               #      numbers with fractional parts
true
> type(5, integer);              # integers  positive whole numbers
true
> type(-5, integer);             #      and negative whole numbers
true
> type(5.5, integer);            # no fractions allowed in type integer
false
> type(99, posint);              # positive integers are integers
                                 #      greater than zero
true
> type(0, posint);               # 0 is not a positive integer
false
> type('hello', string);         # strings surrounded with single quotes
true
> type(true, boolean);           # true and false are booleans
true
> type(5.0, integer);            # maybe this one is a bit of a surprise
true

In fact, even variable names have a type associated with them. Any legal variable name which has not been assigned a value in Darwin has type name.

> type(this_is_the_longest_variable_name_in_the_world, name);
> type(R2D2, name);
> type(_toying_with_disaster, name);
(see § [*] for the criteria of what constitutes a legal name).

The type boolean has two values: true and false. The real and integer types correspond to the mathematical concepts of a real and integer numbers respectively. The posint type is an abbreviation of positive integer. The posint type does not include the number zero. The relationships between these three types is shown in Figure [*].


  
Figure: The relationships between the different number types. Every object of type integer also has type real. Every object of type posint also has type integer (and hence type real).
\begin{figure}\centerline{\psfig{file=Diagrams/number.ps}}
\end{figure}

An object of type string is a sequence of symbols surrounded by the single quote symbols (').

From these simple types, we can build the more complex structured types that we will encounter in later chapters of this book.



 
next up previous contents
Next: Sets Up: Exploring the Basics Previous: Variables
Gaston Gonnet
1998-09-15