4.2 Booleans

► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

▼ | 4 | Datatypes

4.1 | Equality
4.2 | Booleans
4.3 | Numbers
4.4 | Strings
4.5 | Byte Strings
4.6 | Characters
4.7 | Symbols
4.8 | Regular Expressions
4.9 | Keywords
4.10 | Pairs and Lists
4.11 | Mutable Pairs and Lists
4.12 | Vectors
4.13 | Stencil Vectors
4.14 | Boxes
4.15 | Hash Tables
4.16 | Treelists
4.17 | Sequences and Streams
4.18 | Dictionaries
4.19 | Sets
4.20 | Procedures
4.21 | Void
4.22 | Undefined

► | 4.2 | Booleans

4.2.1 | Boolean Aliases
4.2.2 | Mutability Predicates

On this page:

boolean?
not
immutable?
4.2.1 Boolean Aliases
true
false
symbol=?
boolean=?
false?
nand
nor
implies
xor
4.2.2 Mutability Predicates
mutable-string?
immutable-string?
mutable-bytes?
immutable-bytes?
mutable-vector?
immutable-vector?
mutable-box?
immutable-box?
mutable-hash?
immutable-hash?

top contents ← prev up next →

### 4.2 Booleans🔗ℹ

True and false booleans are represented by the values #t and #f, respectively, though operations that depend on a boolean value typically treat anything other than #f as true. The #t value is always eq? to itself, and #f is always eq? to itself.

See Reading Booleans for information on reading booleans and Printing Booleans for information on printing booleans.

See also and, or, andmap, and ormap.

> procedure (boolean? v) → boolean?
> v : any/c

Returns #t if v is #t or #f, #f otherwise.

Examples:

> > (boolean? #f)
> #t
> > (boolean? #t)
> #t
> > (boolean? 'true)
> #f

> procedure (not v) → boolean?
> v : any/c

Returns #t if v is #f, #f otherwise.

Examples:

> > (not #f)
> #t
> > (not #t)
> #f
> > (not 'we-have-no-bananas)
> #f

> procedure (immutable? v) → boolean?
> v : any/c

Returns #t if v is an immutable string, byte string, vector, hash table, or box, #f otherwise.

Note that immutable? is not a general predicate for immutability (despite its name). It works only for a handful of datatypes for which a single predicate—string?, vector?, etc.—recognizes both mutable and immutable variants of the datatype. In particular, immutable? produces #f for a pair, even though pairs are immutable, since pair? implies immutability.

See also immutable-string?, mutable-string?, etc.

Examples:

> > (immutable? 'hello)
> #f
> > (immutable? "a string")
> #t
> > (immutable? (box 5))
> #f
> > (immutable? #(0 1 2 3))
> #t
> > (immutable? (make-hash))
> #f
> > (immutable? (make-immutable-hash '([a b])))
> #t
> > (immutable? #t)
> #f

#### 4.2.1 Boolean Aliases🔗ℹ

(require racket/bool) | package: base

The bindings documented in this section are provided by the racket/bool and racket libraries, but not racket/base.

> value true : boolean?

An alias for #t.

> value false : boolean?

An alias for #f.

> procedure (symbol=? a b) → boolean?
> a : symbol?
> b : symbol?

Returns (equal? a b) (if a and b are symbols).

> procedure (boolean=? a b) → boolean?
> a : boolean?
> b : boolean?

Returns (equal? a b) (if a and b are booleans).

> procedure (false? v) → boolean?
> v : any/c

Returns (not v).

> syntax (nand expr ...)

Same as (not (and expr ...)).

Examples:

> > (nand #f #t)
> #t
> > (nand #f (error 'ack "we don't get here"))
> #t

> syntax (nor expr ...)

Same as (not (or expr ...)).

In the two argument case, returns #t if neither of the arguments is a true value.

Examples:

> > (nor #f #t)
> #f
> > (nor #t (error 'ack "we don't get here"))
> #f

> syntax (implies expr1 expr2)

Checks to be sure that the first expression implies the second.

Same as (if expr1 expr2 #t).

Examples:

> > (implies #f #t)
> #t
> > (implies #f #f)
> #t
> > (implies #t #f)
> #f
> > (implies #f (error 'ack "we don't get here"))
> #t

> procedure (xor b1 b2) → any
> b1 : any/c
> b2 : any/c

Returns the exclusive or of b1 and b2.

If exactly one of b1 and b2 is not #f, then return it. Otherwise, returns #f.

Examples:

> > (xor 11 #f)
> 11
> > (xor #f 22)
> 22
> > (xor 11 22)
> #f
> > (xor #f #f)
> #f

#### 4.2.2 Mutability Predicates🔗ℹ

(require racket/mutability) | package: base

The bindings documented in this section are provided by the racket/mutability library, not racket/base or racket.

Added in version 8.9.0.3 of package base.

> procedure (mutable-string? v) → boolean?
> v : any/c
> procedure (immutable-string? v) → boolean?
> v : any/c
> procedure (mutable-bytes? v) → boolean?
> v : any/c
> procedure (immutable-bytes? v) → boolean?
> v : any/c
> procedure (mutable-vector? v) → boolean?
> v : any/c
> procedure (immutable-vector? v) → boolean?
> v : any/c
> procedure (mutable-box? v) → boolean?
> v : any/c
> procedure (immutable-box? v) → boolean?
> v : any/c
> procedure (mutable-hash? v) → boolean?
> v : any/c
> procedure (immutable-hash? v) → boolean?
> v : any/c

Predicates that combine string?, bytes?, vector?, box?, and hash? with immutable? or its inverse. The predicates are potentially faster than using immutable? and other predicates separately.
top contents ← prev up next →