Predicate Procedures¶
Overview¶
Predicate procedures are procedures that test an object for a specific property
and return a boolean value — either #t (true) or #f (false). By
convention, the names of predicate procedures end with a question mark ?.
Type Predicates
The most fundamental predicates are the type predicates, which test whether
an object belongs to a particular type. Every first-class type in this
implementation has a corresponding type predicate: number?, boolean?,
null?, pair?, list?, procedure?, symbol?, string?,
char?, vector?, bytevector?, port?, set?, hash?, and
eof-object?. These are the primary tool for runtime type dispatch and
defensive programming.
Note that list? is stricter than pair?: a pair is any cons cell,
whereas a list is specifically a chain of pairs terminated by the empty list
(). An improper list such as (1 . 2) satisfies pair? but not
list?. The empty list () satisfies null? and list? but not
pair?.
Numeric Tower Predicates
The numeric type predicates — complex?, real?, rational?,
integer?, exact-integer?, and bigint? — reflect the structure of
the numeric tower. The tower is hierarchical: every integer is rational, every
rational is real, and every real is complex. Consequently these predicates are
not mutually exclusive: (rational? 42) and (integer? 42) are both
#t. The key distinctions are:
complex?is equivalent tonumber?— all numbers are complex.real?returns#tfor any number with a zero imaginary part.rational?returns#tfor exact numbers and finite inexact reals.integer?follows the tower definition:(integer? 3.0)is#tsince3.0has no fractional part, even though it is inexact.exact-integer?is the strictest integer test: it returns#tonly for exact integers, excluding inexact integers like3.0.bigint?distinguishes arbitrary-precision integers from native 64-bit integers; note that both satisfyinteger?andexact-integer?.
The exactness predicates exact? and inexact? test whether a number’s
value is represented exactly. Exact numbers include all integers, rationals,
and bignums. Inexact numbers include all floating-point reals and any complex
number with at least one inexact component.
Numeric Value Predicates
The numeric value predicates — zero?, positive?, negative?,
odd?, and even? — test properties of a number’s value rather than its
type. positive? and negative? require a real argument and will raise an
error if given a complex number with a non-zero imaginary part, since the
concepts of positive and negative are not defined for the complex numbers.
odd? and even? require an integer argument per the numeric tower
definition, which means (odd? 3.0) is valid and returns #t.
Boolean Value Predicates
The procedures true? and false? test for the literal boolean objects
#t and #f respectively. These are distinct from the general notion of
truthiness used in conditional expressions: in Scheme, every value except
#f is considered true in a boolean context, so (if 0 'yes 'no) yields
'yes. By contrast, (true? 0) returns #f, since 0 is not the
literal #t object. These procedures are not part of R7RS and are provided
as a convenience.
Predicate Procedures¶
Type-Identity Procedures¶
number?¶
- (number? obj)
Returns
#tif obj is a number of any numeric type (integer, rational, real, or complex),#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a number,#fotherwise.- Return type:
boolean
Example:
--> (number? 42) #t --> (number? 1/3) #t --> (number? 3.14) #t --> (number? 1+2i) #t --> (number? "42") #f
boolean?¶
- (boolean? obj)
Returns
#tif obj is a boolean (#tor#f),#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a boolean,#fotherwise.- Return type:
boolean
Example:
--> (boolean? #t) #t --> (boolean? #f) #t --> (boolean? 0) #f
null?¶
- (null? obj)
Returns
#tif obj is the empty list(),#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is the empty list,#fotherwise.- Return type:
boolean
Example:
--> (null? '()) #t --> (null? '(1 2 3)) #f --> (null? #f) #f
pair?¶
- (pair? obj)
Returns
#tif obj is a pair,#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a pair,#fotherwise.- Return type:
boolean
Example:
--> (pair? '(1 2)) #t --> (pair? '(1 . 2)) #t --> (pair? '()) #f --> (pair? 42) #f
list?¶
- (list? obj)
Returns
#tif obj is a proper list,#fotherwise. A proper list is either the empty list or a chain of pairs terminated by the empty list. Improper lists and circular lists return#f.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a proper list,#fotherwise.- Return type:
boolean
Example:
--> (list? '(1 2 3)) #t --> (list? '()) #t --> (list? '(1 . 2)) #f --> (list? 42) #f
procedure?¶
- (procedure? obj)
Returns
#tif obj is a procedure (either a lambda or a built-in procedure),#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a procedure,#fotherwise.- Return type:
boolean
Example:
--> (procedure? car) #t --> (procedure? (lambda (x) x)) #t --> (procedure? 42) #f
symbol?¶
- (symbol? obj)
Returns
#tif obj is a symbol,#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a symbol,#fotherwise.- Return type:
boolean
Example:
--> (symbol? 'foo) #t --> (symbol? "foo") #f --> (symbol? 42) #f
string?¶
- (string? obj)
Returns
#tif obj is a string,#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a string,#fotherwise.- Return type:
boolean
Example:
--> (string? "hello") #t --> (string? #\h) #f --> (string? 'hello) #f
char?¶
- (char? obj)
Returns
#tif obj is a character,#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a character,#fotherwise.- Return type:
boolean
Example:
--> (char? #\a) #t --> (char? "a") #f --> (char? 97) #f
vector?¶
- (vector? obj)
Returns
#tif obj is a vector,#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a vector,#fotherwise.- Return type:
boolean
Example:
--> (vector? #(1 2 3)) #t --> (vector? '(1 2 3)) #f --> (vector? 42) #f
bytevector?¶
- (bytevector? obj)
Returns
#tif obj is a bytevector,#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a bytevector,#fotherwise.- Return type:
boolean
Example:
--> (bytevector? #u8(1 2 3)) #t --> (bytevector? #(1 2 3)) #f --> (bytevector? 42) #f
port?¶
- (port? obj)
Returns
#tif obj is a port,#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a port,#fotherwise.- Return type:
boolean
Example:
--> (port? (current-input-port)) #t --> (port? "myfile.txt") #f
set?¶
- (set? obj)
Returns
#tif obj is a set,#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a set,#fotherwise.- Return type:
boolean
Example:
--> (set? #{1 2 3}) #t --> (set? (set 1 2 3)) #t --> (set? '(1 2 3)) #f --> (set? #[1 "one"]) #f
hash?¶
- (hash? obj)
Returns
#tif obj is a hash,#fotherwise.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a hash,#fotherwise.- Return type:
boolean
Example:
--> (hash? #["a" 1 "b" 2]) #t --> (hash? (hash "a" 1 "b" 2)) #t --> (hash? #{1 2 3}) #f --> (hash? '((a . 1) (b . 2))) #f
eof-object?¶
- (eof-object? obj)
Returns
#tif obj is the end-of-file object,#fotherwise. The end-of-file object is returned by input procedures when the end of an input stream is reached.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is the end-of-file object,#fotherwise.- Return type:
boolean
Example:
--> (eof-object? (read (open-input-string ""))) #t --> (eof-object? "") #f
Numeric identity procedures¶
exact?¶
- (exact? z)
Returns
#tif z is an exact number,#fotherwise. For complex numbers, returns#tonly if both the real and imaginary parts are exact.- Parameters:
z (number) – A number to test.
- Returns:
#tif z is exact,#fotherwise.- Return type:
boolean
Example:
--> (exact? 42) #t --> (exact? 1/3) #t --> (exact? 3.14) #f --> (exact? 1+2i) #t --> (exact? 1+2.0i) #f
inexact?¶
- (inexact? z)
Returns
#tif z is an inexact number,#fotherwise. For complex numbers, returns#tif either the real or imaginary part is inexact.- Parameters:
z (number) – A number to test.
- Returns:
#tif z is inexact,#fotherwise.- Return type:
boolean
Example:
--> (inexact? 3.14) #t --> (inexact? 42) #f --> (inexact? 1/3) #f --> (inexact? 1.0+2.0i) #t
complex?¶
- (complex? obj)
Returns
#tif obj is a number,#fotherwise. Per R7RS, all numbers are complex numbers, so this procedure is equivalent tonumber?.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a number,#fotherwise.- Return type:
boolean
Example:
--> (complex? 42) #t --> (complex? 1/3) #t --> (complex? 1+2i) #t --> (complex? "hello") #f
real?¶
- (real? obj)
Returns
#tif obj is a real number,#fotherwise. Integers, rationals, and reals all return#t. A complex number returns#tonly if its imaginary part is exactly zero.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a real number,#fotherwise.- Return type:
boolean
Example:
--> (real? 42) #t --> (real? 1/3) #t --> (real? 3.14) #t --> (real? 1+0i) #t --> (real? 1+2i) #f --> (real? "hello") #f
rational?¶
- (rational? obj)
Returns
#tif obj is a rational number,#fotherwise. Exact integers and rationals always return#t. Inexact reals return#tif they are finite. A complex number returns#tonly if its real part is exact and its imaginary part is zero. Non-numbers return#fwithout raising an error.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a rational number,#fotherwise.- Return type:
boolean
Example:
--> (rational? 42) #t --> (rational? 1/3) #t --> (rational? 3.14) #t --> (rational? +inf.0) #f --> (rational? +nan.0) #f --> (rational? "hello") #f
integer?¶
- (integer? obj)
Returns
#tif obj is an integer,#fotherwise. This follows the R7RS numeric tower definition: a real number with no fractional part is considered an integer, so(integer? 3.0)returns#t. Non-numbers return#fwithout raising an error.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is an integer,#fotherwise.- Return type:
boolean
Example:
--> (integer? 42) #t --> (integer? 3.0) #t --> (integer? 3.5) #f --> (integer? 1/3) #f --> (integer? "hello") #f
exact-integer?¶
- (exact-integer? obj)
Returns
#tif obj is both an exact number and an integer,#fotherwise. Unlikeinteger?, this returns#ffor inexact integers such as3.0.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is an exact integer,#fotherwise.- Return type:
boolean
Example:
--> (exact-integer? 42) #t --> (exact-integer? 3.0) #f --> (exact-integer? 1/3) #f --> (exact-integer? "hello") #f
bigint?¶
- (bigint? obj)
Returns
#tif obj is a bignum (an exact integer too large to be represented as a native 64-bit integer),#fotherwise. This is a non-standard predicate specific to this implementation.Note that
(integer? obj)and(exact-integer? obj)both return#tfor bignums;bigint?is provided for the rare cases where code needs to distinguish between native integers and arbitrary-precision ones.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is a bignum,#fotherwise.- Return type:
boolean
Example:
--> (bigint? 42) #f --> (bigint? 99999999999999999999999999999) #t --> (bigint? (expt 2 128)) #t --> (bigint? 1/3) #f
Numeric predicate procedures¶
zero?¶
- (zero? z)
Returns
#tif z is zero,#fotherwise. For complex numbers, returns#tonly if both the real and imaginary parts are zero.- Parameters:
z (number) – A number to test.
- Returns:
#tif z is zero,#fotherwise.- Return type:
boolean
Example:
--> (zero? 0) #t --> (zero? 0.0) #t --> (zero? 0+0i) #t --> (zero? 1) #f
positive?¶
- (positive? x)
Returns
#tif x is greater than zero,#fotherwise. x must be a real number; an error is raised if x is a complex number with a non-zero imaginary part.- Parameters:
x (real) – A real number to test.
- Returns:
#tif x is greater than zero,#fotherwise.- Return type:
boolean
Example:
--> (positive? 1) #t --> (positive? 0) #f --> (positive? -1) #f --> (positive? 0.1) #t
negative?¶
- (negative? x)
Returns
#tif x is less than zero,#fotherwise. x must be a real number; an error is raised if x is a complex number with a non-zero imaginary part.- Parameters:
x (real) – A real number to test.
- Returns:
#tif x is less than zero,#fotherwise.- Return type:
boolean
Example:
--> (negative? -1) #t --> (negative? 0) #f --> (negative? 1) #f --> (negative? -0.1) #t
odd?¶
- (odd? n)
Returns
#tif n is an odd integer,#fotherwise. n must be an integer; an error is raised if n is a non-integer number such as a rational or inexact real with a fractional part.- Parameters:
n (integer) – An integer to test.
- Returns:
#tif n is odd,#fotherwise.- Return type:
boolean
Example:
--> (odd? 1) #t --> (odd? 2) #f --> (odd? -3) #t --> (odd? 3.0) #t
even?¶
- (even? n)
Returns
#tif n is an even integer,#fotherwise. n must be an integer; an error is raised if n is a non-integer number such as a rational or inexact real with a fractional part.- Parameters:
n (integer) – An integer to test.
- Returns:
#tif n is even,#fotherwise.- Return type:
boolean
Example:
--> (even? 2) #t --> (even? 3) #f --> (even? 0) #t --> (even? 4.0) #t
Boolean Predicates¶
false?¶
- (false? obj)
Returns
#tif obj is the literal boolean#f,#fotherwise. Note that this tests for the literal#fobject specifically, not general falsiness — in this implementation, as in R7RS,#fis the only false value, but this procedure is distinct from simply usingnot.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is#f,#fotherwise.- Return type:
boolean
Example:
--> (false? #f) #t --> (false? #t) #f --> (false? 0) #f --> (false? '()) #f
true?¶
- (true? obj)
Returns
#tif obj is the literal boolean#t,#fotherwise. Note that this tests for the literal#tobject specifically, not general truthiness — any non-#fvalue is truthy in Scheme, but this procedure returns#tonly for the boolean#titself.- Parameters:
obj (any) – The object to test.
- Returns:
#tif obj is#t,#fotherwise.- Return type:
boolean
Example:
--> (true? #t) #t --> (true? #f) #f --> (true? 1) #f --> (true? '(1 2 3)) #f