ZetCode

Scheme data types

last modified November 24, 2026 Guile 3.0

Scheme is dynamically typed: types belong to values, not to variables. A variable can hold a number now and a string later, and the type of a value is checked when an operation uses it. The language has a small set of built-in types, and each has a predicate, a procedure whose name ends in a question mark and which returns #t or #f. This article introduces the types one at a time, with GNU Guile 3.0.7.

Booleans

The two boolean values are written #t and #f, or #true and #false in the longer R7RS spelling. The rule for truth is simple: #f is the only false value, and everything else counts as true in a test.

booleans.scm
(write #t)
(newline)
(write #false)
(newline)
(write (boolean? #f))
(newline)
(write (boolean? 0))
(newline)
(write (if 0 'true 'false))
(newline)
(write (if '() 'true 'false))
(newline)
(write (if "" 'true 'false))
(newline)
(write (if #f 'true 'false))
(newline)

write prints the long forms in the short notation. boolean? is true only for the two boolean values, so it returns #f for 0. The if tests show that 0, the empty list '(), and the empty string are all true, unlike in C, Python, or older Lisps. Only #f selects the second branch.

$ guile booleans.scm
#t
#f
#t
#f
true
true
true
false

Numbers

Scheme numbers form a numeric tower of integers, rationals, reals, and complex numbers. Independently of that, every number is either exact or inexact. Integers and fractions are exact and have unlimited size, while numbers with a decimal point are inexact floating-point values. The program prints a few numbers with two of their properties.

numbers.scm
(define (show-number n)
  (write n)
  (display " integer? ")
  (write (integer? n))
  (display " exact? ")
  (write (exact? n))
  (newline))

(show-number 42)
(show-number -7)
(show-number 2/3)
(show-number 2.5)
(show-number 3.0)
(show-number (expt 2 70))
(write (number? "42"))
(newline)

show-number prints the number and the results of integer? and exact?. The fraction 2/3 is exact but not an integer. 3.0 is an integer in value, but it is inexact because it was written with a decimal point. (expt 2 70) exceeds the 64-bit range and is still an exact integer. The string "42" is not a number, as number? shows. The numbers article covers the numeric tower in detail.

$ guile numbers.scm
42 integer? #t exact? #t
-7 integer? #t exact? #t
2/3 integer? #f exact? #t
2.5 integer? #f exact? #f
3.0 integer? #t exact? #f
1180591620717411303424 integer? #t exact? #t
#f

Characters

A character literal starts with #\: #\a is the letter a and #\7 is the digit seven as a character. Characters that are hard to see have names, such as #\space and #\newline. Guile characters are Unicode code points, and char->integer returns the code point number.

characters.scm
(define (show-char c)
  (write c)
  (display " displays as [")
  (display c)
  (display "] code ")
  (display (char->integer c))
  (newline))

(show-char #\a)
(show-char #\Z)
(show-char #\7)
(show-char #\space)
(show-char #\λ)
(write (char? #\a))
(newline)
(write (char? "a"))
(newline)

write shows each character in its literal form and display shows the character itself, so the space appears between the brackets. The codes are the Unicode values: 97 for a, 55 for the digit 7, and 955 for the Greek letter lambda. A one-letter string is not a character, so (char? "a") is #f.

$ guile characters.scm
#\a displays as [a] code 97
#\Z displays as [Z] code 90
#\7 displays as [7] code 55
#\space displays as [ ] code 32
#\λ displays as [λ] code 955
#t
#f

Strings

A string is a sequence of characters in double quotes. Strings have a length, their characters are numbered from zero, and many procedures produce new strings from old ones. The basics article showed the escapes \" and \n.

strings.scm
(define city "Bratislava")

(write city)
(newline)
(display (string-length city))
(newline)
(write (string-ref city 0))
(newline)
(write (string-upcase city))
(newline)
(write (string-append city ", " "Slovakia"))
(newline)
(write (string? city))
(newline)
(write (string? 'city))
(newline)

string-length counts the characters, and (string-ref city 0) returns the first one as a character. string-upcase and string-append return new strings and leave city unchanged. The quoted name 'city is a symbol, not a string, so string? returns #f for it.

$ guile strings.scm
"Bratislava"
10
#\B
"BRATISLAVA"
"Bratislava, Slovakia"
#t
#f

Symbols

A symbol is a name used as a value. It is written as a quoted identifier such as 'green. Two symbols with the same spelling are the same object, so they can be compared with the fast identity test eq?. Symbols are typically used as keys, tags, and enumeration values.

symbols.scm
(define colour 'green)

(write colour)
(newline)
(write (symbol? colour))
(newline)
(write (symbol? "green"))
(newline)
(write (eq? colour 'green))
(newline)
(write (eq? 'Green 'green))
(newline)
(write (symbol->string colour))
(newline)
(write (string->symbol "blue"))
(newline)

colour holds the symbol green, which write prints without the apostrophe. The quote is part of the source code, not of the symbol. eq? confirms that the two spellings of green are one object, and it also shows that symbols are case-sensitive: 'Green is a different symbol. symbol->string and string->symbol convert between the two types.

$ guile symbols.scm
green
#t
#f
#t
#f
"green"
blue

Pairs and lists

A pair holds two values, created with cons and read with car (the first part) and cdr (the second part). A list is a chain of pairs whose last cdr is the empty list '(). Lists are the central data structure of Scheme; they are written as quoted literals or built with list.

lists.scm
(define primes '(2 3 5 7 11))
(define mixed (list 1 "two" #\3 'four 5.0))
(define pair (cons 'x 10))

(write primes)
(newline)
(write mixed)
(newline)
(write pair)
(newline)
(write (car primes))
(newline)
(write (cdr primes))
(newline)
(write (cons 1 primes))
(newline)
(write (length mixed))
(newline)
(write (list? primes))
(display " ")
(write (pair? pair))
(display " ")
(write (list? pair))
(newline)
(write '())
(display " ")
(write (null? '()))
(display " ")
(write (null? primes))
(newline)

A list may mix values of any type. A pair whose second part is not a list is printed with a dot, as in (x . 10). car of a list is its first element and cdr is the rest of the list, and cons adds an element at the front. The predicates show the difference: a list is also a pair, but the dotted pair is not a list. null? is true only for the empty list. The pairs and lists article covers these operations in depth.

$ guile lists.scm
(2 3 5 7 11)
(1 "two" #\3 four 5.0)
(x . 10)
2
(3 5 7 11)
(1 2 3 5 7 11)
5
#t #t #f
() #t #f

Vectors

A vector is a fixed-length sequence whose elements are reached by index in constant time, like an array in other languages. A vector literal is written #(1 2 3), and vector builds one from its arguments. Indexes start at zero.

vectors.scm
(define scores (vector 90 75 82))
(define grid #(1 2 3))

(write scores)
(newline)
(write (vector-ref scores 1))
(newline)
(write (vector-length scores))
(newline)
(vector-set! scores 1 100)
(write scores)
(newline)
(write (vector? grid))
(display " ")
(write (vector? '(1 2 3)))
(newline)

vector-ref reads an element and vector-length returns the size. vector-set! replaces an element in place; the exclamation mark is the Scheme convention for a procedure that modifies its argument. The vector is created with vector, because a literal such as #(1 2 3) is a constant and must not be modified. A list with the same elements is not a vector.

$ guile vectors.scm
#(90 75 82)
75
3
#(90 100 82)
#t #f

Procedures

Procedures are values too, and procedure? recognises them. Built-in procedures and procedures created with define are values of the same type.

procs.scm
(define (double x) (* 2 x))

(write (procedure? double))
(newline)
(write (procedure? car))
(newline)
(write (procedure? 'double))
(newline)
(display double)
(newline)
(write (double 21))
(newline)

double and the built-in car are both procedures, while the quoted 'double is only a symbol that happens to have the same spelling. display shows a procedure with its name and parameter list.

$ guile procs.scm
#t
#t
#f
#<procedure double (x)>
42

Telling types apart

The predicates can be combined into a procedure that names the type of any value. The types tested here do not overlap: no value satisfies two of these predicates, so the order of the tests does not change the result. A non-empty list is reported as a pair, because it is built from pairs, while the empty list has a type of its own. The procedure nests one if inside the next.

typeof.scm
(define (type-of value)
  (if (boolean? value) 'boolean
      (if (number? value) 'number
          (if (char? value) 'char
              (if (string? value) 'string
                  (if (symbol? value) 'symbol
                      (if (null? value) 'empty-list
                          (if (pair? value) 'pair
                              (if (vector? value) 'vector
                                  (if (procedure? value) 'procedure
                                      'other))))))))))

(define (show value)
  (write value)
  (display " has type ")
  (display (type-of value))
  (newline))

(show #t)
(show 3/4)
(show #\q)
(show "text")
(show 'name)
(show '())
(show '(1 2))
(show #(1 2))
(show type-of)

Each if returns a symbol when its test succeeds and otherwise passes the value on to the next test. The nesting is hard to read, and the cond form, covered in the conditionals article, expresses the same chain of tests without it. The output shows one value of each type, with the procedure type-of itself as the last one.

$ guile typeof.scm
#t has type boolean
3/4 has type number
#\q has type char
"text" has type string
name has type symbol
() has type empty-list
(1 2) has type pair
#(1 2) has type vector
#<procedure type-of (value)> has type procedure

Source

Data types — GNU Guile Reference Manual

R7RS: the Revised7 Report on Scheme

In this article we have met the built-in types of Scheme: booleans and the rule that only #f is false, exact and inexact numbers, characters, strings, symbols, pairs and lists, vectors, and procedures, and we have used their predicates to tell values apart. The variables and binding article shows how names are bound to these values, and the numbers article examines the numeric tower.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Scheme tutorials.