Scheme booleans and equality
last modified November 24, 2026 Guile 3.0
Scheme has two boolean values, #t and #f, and a
simple rule for truth: #f is false and every other value is true.
This article shows how comparisons produce booleans, how not,
and, and or combine them, and how the language tests
whether two values are equal. Scheme has several equality predicates, and
choosing the right one depends on what kind of sameness is meant. The examples
run with GNU Guile 3.0.7.
Comparisons return booleans
The data types article introduced the two
boolean values. Most booleans in a program come from comparisons and
predicates. The numeric comparisons <, >,
<=, >=, and = accept more than two
arguments, which makes range tests short.
(define temperature 21) (define warm (> temperature 20)) (write warm) (newline) (write (boolean? warm)) (newline) (write (< 1 2 3 4)) (newline) (write (< 1 3 2 4)) (newline) (write (<= 18 temperature 25)) (newline) (write (= 7 7 7)) (newline)
A boolean is an ordinary value: warm stores the result of a
comparison, and boolean? confirms its type. With several
arguments, < is true only when each number is smaller than the
next one, so (< 1 3 2 4) is false. (<= 18 temperature
25) reads as the mathematical 18 ≤ t ≤ 25, and =
checks that all its arguments are equal.
$ guile comparisons.scm #t #t #t #f #t #t
Negation with not
not is an ordinary procedure that returns #t when its
argument is #f and #f for any other value. It follows
the truth rule of the language, so it does not treat 0 or the empty list as
false.
(write (not #t)) (newline) (write (not #f)) (newline) (write (not 0)) (newline) (write (not '())) (newline) (write (not (> 3 5))) (newline)
Because 0 and '() are true values, not returns
#f for both. The last line negates the result of a comparison,
which is the usual way not appears in programs.
$ guile negation.scm #f #t #f #f #t
Conjunction with and
and evaluates its operands from left to right. If one of them is
#f, it stops and returns #f; otherwise it returns the
value of the last operand. The result is therefore not always a boolean.
and is a special form, not a procedure, because it does not
evaluate all of its operands.
(define (in-range? n low high) (and (>= n low) (<= n high))) (write (in-range? 5 1 10)) (newline) (write (in-range? 15 1 10)) (newline) (write (and 1 2 3)) (newline) (write (and 1 #f 3)) (newline) (write (and)) (newline) (define (safe-divide a b) (and (not (= b 0)) (/ a b))) (write (safe-divide 10 4)) (newline) (write (safe-divide 10 0)) (newline)
in-range? combines two comparisons. (and 1 2 3) shows
that the result is the last value, 3, rather than #t, and
(and) with no operands is #t. safe-divide
uses both properties: when the divisor is zero, and stops at the
first test and returns #f, so the division is never attempted;
otherwise it returns the quotient, the exact 5/2.
$ guile conjunction.scm #t #f 3 #f #t 5/2 #f
Disjunction with or
or also evaluates from left to right, but it stops at the first
operand that is true and returns that value. If every operand is
#f, the result is #f. This makes or a
convenient way to supply a default value.
(define (weekend? day) (or (eq? day 'saturday) (eq? day 'sunday))) (write (weekend? 'sunday)) (newline) (write (weekend? 'monday)) (newline) (write (or #f 2 3)) (newline) (write (or #f #f)) (newline) (write (or)) (newline) (define (parse-count text) (or (string->number text) 0)) (write (parse-count "42")) (newline) (write (parse-count "many")) (newline)
weekend? compares the symbol with two names, using the
eq? predicate explained later in the article. (or #f 2
3) returns 2, the first true value, and (or) with no
operands is #f. string->number returns
#f when the text is not a number, so parse-count
yields either the parsed number or the default 0.
$ guile disjunction.scm #t #f 2 #f #f 42 0
Short-circuit evaluation
The operands that and and or do not need are never
evaluated. A helper procedure that prints a message before returning its value
makes the order of evaluation visible.
(define (check label value) (display "checking ") (display label) (newline) value) (write (and (check 'a #t) (check 'b #f) (check 'c #t))) (newline) (write (or (check 'a #f) (check 'b 42) (check 'c #t))) (newline) (define items '()) (write (and (pair? items) (car items))) (newline)
check announces each operand as it is evaluated. In the
and expression, operand b is false, so c is never checked. In the
or expression, b returns 42, which ends the evaluation. The last
expression shows a common guard: (car items) would fail on the
empty list, but it runs only when pair? has confirmed that there
is a first element.
$ guile shortcircuit.scm checking a checking b #f checking a checking b 42 #f
Numeric equality with =
= compares numbers by their mathematical value, regardless of
whether they are exact or inexact. It works only on numbers; for other values
one of the general equality predicates must be used.
(write (= 2 2.0)) (newline) (write (= 1/2 0.5)) (newline) (write (= 0.1 1/10)) (newline) (write (= (+ 0.1 0.2) 0.3)) (newline) (write (= 'red 'red)) (newline)
2 and 2.0 are equal numbers, and so are 1/2 and 0.5, whose binary floating-point
form is exact. The decimal 0.1 has no exact binary representation, so it differs
from the exact fraction 1/10, and the sum of 0.1 and 0.2 is not exactly 0.3.
Floating-point values are usually compared with a tolerance instead. The last
line passes symbols to =, and the program stops with a wrong type
error. The backtrace is left out.
$ guile numequal.scm #t #t #f #f numequal.scm:9:7: In procedure =: Wrong type argument in position 1: red
Identity with eq?
eq? is the fastest equality test: it returns #t when
both arguments are the same object in memory. Symbols with the same spelling,
the empty list, and the booleans are always the same object. In Guile, small
integers and characters are also stored directly in the value, so
eq? works for them, but the standard does not guarantee this.
(define (make-pair) (list 1 2)) (define a (make-pair)) (define b (make-pair)) (define c a) (write (eq? 'apple 'apple)) (newline) (write (eq? '() '())) (newline) (write (eq? #\x #\x)) (newline) (write (eq? a b)) (newline) (write (eq? a c)) (newline) (write (eq? car car)) (newline)
make-pair builds a new list each time it is called, so
a and b have the same contents but are two different
objects, and eq? returns #f. c is bound
to the same object as a, so they are eq?. Procedures
are compared by identity too, and car is the same procedure as
itself. The lists are built by a procedure on purpose: Guile's compiler may
store two identical literals such as '(1 2) as one constant, which
would make them eq?.
$ guile identity.scm #t #t #t #f #t #t
Equivalence with eqv?
eqv? extends eq? to numbers and characters: two
numbers are eqv? when they have the same value and the same
exactness. It is the predicate that Scheme uses by default, for example in the
case form described in the article on conditionals.
(define (big n) (expt 10 n)) (define (half x) (/ x 2)) (write (eq? (big 30) (big 30))) (newline) (write (eqv? (big 30) (big 30))) (newline) (write (eqv? (half 5.0) (half 5.0))) (newline) (write (eqv? 2 2.0)) (newline) (write (eqv? (list 1) (list 1))) (newline)
Large integers are allocated objects, so two equal results of
expt are not eq?, but they are eqv?. The
same holds for inexact numbers computed at run time. 2 and 2.0 are equal by
=, but eqv? returns #f because one is
exact and the other is not. For pairs, eqv? still compares
identity, so two fresh lists are not equivalent.
$ guile values.scm #f #t #t #f #f
Structural equality with equal?
equal? compares the contents of values. Two strings, lists, or
vectors are equal? when they have the same length and their
elements are equal?; other values are compared with
eqv?. It is the predicate to use when the question is whether two
values look the same when printed.
(define (make-word) (string #\o #\a #\k)) (write (eqv? (make-word) (make-word))) (newline) (write (equal? (make-word) (make-word))) (newline) (write (equal? (list 1 (list 2 3)) (list 1 (list 2 3)))) (newline) (write (equal? (vector 'a 'b) (vector 'a 'b))) (newline) (write (equal? '(1 2) '(1 2.0))) (newline) (write (equal? "Oak" "oak")) (newline)
make-word builds a new string with string, so two
calls give different objects with the same characters. eqv? sees
two objects and equal? sees the same text. Nested lists and
vectors are compared element by element. Because the elements are finally
compared with eqv?, the lists (1 2) and (1
2.0) differ, and the comparison of strings is case-sensitive.
$ guile structure.scm #f #t #t #t #f #f
Typed comparisons
Strings and characters have their own comparison procedures. They accept only
values of their type, which documents the intent and catches mistakes, and they
offer ordering and case-insensitive versions that equal? does not
have.
(write (string=? "pine" "pine")) (newline) (write (string=? "Pine" "pine")) (newline) (write (string-ci=? "Pine" "pine")) (newline) (write (string<? "apple" "banana")) (newline) (write (char=? #\a #\a)) (newline) (write (char<? #\a #\b #\c)) (newline) (write (char-ci=? #\A #\a)) (newline) (write (symbol? 'pine)) (newline)
string=? compares text exactly and string-ci=?
ignores the case of letters. string<? orders strings
alphabetically by character codes. The character procedures follow the same
pattern: char=?, char<? with any number of
arguments, and char-ci=?. Symbols need no separate procedure,
because eq? already compares them by name.
$ guile typed.scm #t #f #t #t #t #t #t #t
Choosing an equality predicate
The last program puts the three general predicates side by side. It calls a comparison procedure on pairs of values of different kinds and prints the result of each predicate on one line.
(define (compare label x y) (display label) (display ": eq? ") (write (eq? x y)) (display " eqv? ") (write (eqv? x y)) (display " equal? ") (write (equal? x y)) (newline)) (define (fresh-list) (list 'a 'b)) (define (fresh-string) (string #\h #\i)) (define (big) (expt 2 100)) (define (third x) (/ x 3)) (define shared (fresh-list)) (compare "symbols " 'pine 'pine) (compare "small ints " 42 42) (compare "big ints " (big) (big)) (compare "inexact " (third 1.0) (third 1.0)) (compare "exact/inex. " 2 2.0) (compare "strings " (fresh-string) (fresh-string)) (compare "lists " (fresh-list) (fresh-list)) (compare "same list " shared shared)
The values are produced by procedures so that each comparison involves two
separately created objects; only the last line compares one list with itself.
The table summarises the rules: eq? is right for symbols and for
checking identity, eqv? adds numbers and characters,
equal? compares contents, and = is the choice for
numbers when exactness does not matter. None of the general predicates treat 2
and 2.0 as equal.
$ guile compare-all.scm symbols : eq? #t eqv? #t equal? #t small ints : eq? #t eqv? #t equal? #t big ints : eq? #f eqv? #t equal? #t inexact : eq? #f eqv? #t equal? #t exact/inex. : eq? #f eqv? #f equal? #f strings : eq? #f eqv? #f equal? #t lists : eq? #f eqv? #f equal? #t same list : eq? #t eqv? #t equal? #t
Source
Equality — GNU Guile Reference Manual
R7RS: the Revised7 Report on Scheme
In this article we have produced booleans with comparisons, combined them with
not, and, and or, used short-circuit
evaluation for guards and default values, and compared values with
=, eq?, eqv?, equal?, and
the typed string and character predicates. The
conditionals article uses these tests to
choose between alternatives.
Author
List all Scheme tutorials.