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

"Hello, World!"

even though they have the same characters, while every character

#\H

is the same object.

Atomic typesEdit

BooleansEdit

Booleans are either

#t

(true) or

#f

(false). Booleans are self-evaluating.

NumbersEdit

Numbers are self-evaluating, and are entered as expected. Scheme also supports complex numbers as for instance

0+4i

. Just typing

4i

is interpreted as a symbol in Scheme, beware.

CharactersEdit

Characters are self-evaluating and entered as such

#\a

.

SymbolsEdit

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.

CompoundEdit

Pairs and ListsEdit

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!" . ()))
Last modified on 7 May 2012, at 18:51