Scheme Programming/Scheme Datatypes

Scheme Programming
 ← A taste of Scheme Scheme Datatypes Simple Expressions → 

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 types

edit

Booleans

edit

Booleans are either

#t

(true) or

#f

(false). Booleans are self-evaluating.

Numbers

edit

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.

Characters

edit

Characters are self-evaluating and entered as such

#\a

.

Symbols

edit

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

Pairs and Lists

edit

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

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

Strings are sequences of characters. They are contained by double-quotes.

"This is a string."