Basic Data Types

Atoms

The building blocks of Scheme programs are atoms and lists. Atoms are single objects, and can be either identifiers or literals:

   ; comments begin with semi-colons and go to the end of the line.

   An-Atom         ; the symbol 'An-Atom'
   1234            ; the integer '1234'
   17/23           ; the rational number seventeen twenty-thirds
   3.14            ; the exact rational number 3.14
   #i3.14          ; an inexact real number approximating 3.14
   0+1i            ; complex number equal to (sqrt -1)
   #\a             ; the character 'a'
   "Hello, World!" ; the string "Hello, World!"
   #t              ; special atom for the boolean value 'true'
   #f              ; special atom for the boolean value'false'
  

While Scheme's approach to symbols, characters, strings and non-integer numbers is unusual, it is fairly self-consistent.

Symbols

One of the most confusing aspects of Scheme is the symbole literal type. A symbol is an identifier, roughly equivalent to a variable in most conventional languages, which can be assigned a value consisting of another Scheme literal, a list, or a function. Unlike in most languages, symbols themselves are treated as literals, and can be manipulated using the quote and quasiquote operators; thus, it is possible to assign a symbol the quoted, literal representation of a different symbol.

Numbers

Standard Scheme supports a full 'numeric tower', that is to say, a hierarchy of numbers covering integers, rational fractions, decimal fractions, and complex numbers. It furthermore differentiates between 'exact' numbers, which have a precise binary representation, and 'inexact' numbers, which are approximations of the actual arithmetical value. All forms of numbers are 'big', in the sense that they can be arbitrarily large or small values limited only by the available memory, rather than limited by fixed word widths. Some Scheme implementations allow programmers to specify a fixed precision for integers and decimal fractions, for the sake of efficiency, but this is not part of the Scheme standard.

Characters

Characters in Scheme are prefixed with the escape code #\; for example, the capital 'A' would be represented as #\A, the lowercase 'b' as #\b, and the numeral '5' as #\5. Certain literal character values which have no printable value are represented by special escape values. These include #\space, #\tab, and #\newline.

Strings

Strings in Scheme are enclosed in double quotes, for example, "Hello, World!". Strings are a first-class intrinsic type in Scheme, and are distinct from both characters and vectors.

Booleans

In Scheme, the boolean values are #t, for true, and #f, for false (these should not be confused with the character values #\t and #\f). Unlike in Common Lisp, the empty list, '(), is not considered equal to false, while the null value, nil, is treated as a distinct value from both of them.

Lists

A list is a group of one or more atoms in a pair of parentheses (most versions allow you to substitute brackets or even braces for parentheses, so long as each member of a pair matches the other). For example,

   (a b c)
  

is a list whose members are the symbols 'a', 'b', and 'c' (technically, there is also a null list following the 'c', but that isn't too important right now). Lists can be nested, like so:

   ((a)  (b c) d)
  
In this list, the members are the list '(a)', the list '(b c)', and the atom 'd'.

Vectors

Vectors are Scheme's equivalent of zero-indexed arrays. However, unlike in most languages, vectors are heterogeneous; a given vector can contain literals of different types, other vectors, or even lists For example, to get the second element in a vector from the interpreter prompt, you would enter:

   > (vector-ref  #(1/3 b (#f #\d)) 1)
   b
  

Contents Previous Next