Scheme Programming/Scheme Datatypes
Scheme has several datatypes, there are atomic datatypes and compound datatypes, the significant difference between both is that while atomic datatypes are unique and immutable, compound datatypes are mutable, this means that we can modify them. This also means that there can be for instance two different strings
even though they have the same characters, while every character
is the same object.
Atomic types[edit | edit source]
Booleans[edit | edit source]
Booleans are either
(false). Booleans are self-evaluating.
Numbers[edit | edit source]
Numbers are self-evaluating, and are entered as expected. Scheme also supports complex numbers as for instance
. Just typing
is interpreted as a symbol in Scheme, beware.
Characters[edit | edit source]
Characters are self-evaluating and entered as such
Symbols[edit | edit source]
Symbols are sequences of characters, different from strings because they are not self-evaluating, and they can't contain white-space either. They are just input by typing them really, for instance
(add one two)
is a list in Scheme that contains three symbols.
Compound[edit | edit source]
Pairs and Lists[edit | edit source]
Pairs and lists are the most important compound types in Scheme. We already said that Scheme source code itself made up of lists, thus lists are obviously not self-evaluating. Typing a list which cannot be sensibly evaluated (such as one that starts with a number) will produce an error.
There is one list though which is atomic; it's called the empty list, or nil or null, depending to whom you talk. It's simply the list of no elements at all. It's represented in source code by
. All other lists are in effect pairs in disguise, a pair of a and b is written like
(a . b)
, this is different from a list of a and b. Pairs are also not self-evaluating and often produce an error. What a list is, is just a pair, whose first element (also called its car) is the first element of the list, and whose second element (also called its cdr) is a list containing all the other elements. Thus the list:
(print "Hello, World!")
Is just a shorthand for:
(print . ("Hello, World!" . ()))
Vectors[edit | edit source]
The problem with lists is that list-ref and list-set! are slow operations if the list is very large. For a data structure that allows fast reading and writing, Scheme provides vectors.
Here is an example of a vector of numbers:
#(100 90 80 70)
Strings[edit | edit source]
Strings are sequences of characters. They are contained by double-quotes.
"This is a string."