Types
Skip to end of metadata
Go to start of metadata

built-in types and how to construct aggregate types (e.g., records, classes or structures). The program should demonstrate which of the standard suite of operators (+ - / * %) manipulate which types and what the resulting type of each operation is. Use it to explain the type system in your language.

Built In Types

All entities are atoms or lists. Lists are aggregate types (see next section). Atoms are either symbols or they are not. Symbols are variables (which may be functions).

All other atoms are numbers, characters, and bits.

Numbers are real and complex. Reals are rationals and floating point values. Rationals are integers and ratios.

Integers are different from many languages in that they do not have an explicit bound. It is possible for optimization purposes to restrict an integer value to a fixnum, but the default will automatically change to a bignum if necessary, which is only bounded by the entire memory capacity. Floating point numbers have four options (short, single, double, or long).

Lists are cons types. A cons has a car, which is the first element of the list, and a cdr, which is the rest of the list. This is simply a linked list format.

Characters start with #\ and follow any character you like, since back-slash make them escaped. So, #\g is the character g.

Aggregate Types

Arrays are lists of specified dimension:

An array can carry any type of objects, and it can also be restricted to carry one particular type.

Vectors are one-dimensional arrays:

Strings are vectors whose elements are restricted to type string-char:

Structures create objects which store data. Structures are defined by a special for of Lisp defstruct.

defstruct name slots... - defines a structure type called name (which must be a symbol) with slots specified. Slots can be strings, cons(?), numbers.

Basic Operations

Common Lisp supports basic arithmetic operations such as addition, subtraction, multiplication, and division for the type number using operators *, /, -, +. If arguments in the operation of the same type, then a value of that type will be returned as a result, except for division performed on integers, which would output a rational number. If the arguments of a different type, then a widening is applied to the narrower type.  

Rationals types are a nice feature if you are running mathematical calculations, that require exact precision. The downside is that rational numbers (that are aggregate types of integers) take up more space and slow down the program. If you priority is the speed, not precision, you can specify in the program if you want integer division that evaluates to rationals to be evaluated to floats.

  • The question arises then concerning an operation on integer and floating point types. In Common Lisp integer are not restricted in space allocated for them, they can be as big as we want them. This is a special feature of Common Lisp. Usually an operation on integer and a float would output a float. But what would happen if an operation performed onto a big integer and a float?

A strong typed properties of Common Lisp showed above. The compiler evaluated the number (expt 2 ( expt 2 16))) and then tried to convert it to a single-float, which would not work due to the size of the number, so an error was reported to us. Big integers can have a wider space allocated for them compare to the ones floating points have, so operation on those could not be performed.

Calling an operation on more then two values is the same as calling it onto first two, evaluating the result, calling the operation on that result and the next value and so on:

Labels
  • No labels