198 lines
6.6 KiB
Racket
198 lines
6.6 KiB
Racket
#lang scribble/doc
|
|
@(require "mz.ss")
|
|
|
|
@title[#:tag "booleans"]{Booleans and Equality}
|
|
|
|
True and false @deftech{booleans} are represented by the values
|
|
@scheme[#t] and @scheme[#f], respectively, though operations that
|
|
depend a boolean value typically treat anything other than @scheme[#f]
|
|
as true.
|
|
|
|
See also: @scheme[and], @scheme[or], @scheme[andmap], @scheme[ormap].
|
|
|
|
|
|
@defproc[(boolean? [v any/c]) boolean?]{
|
|
|
|
Returns @scheme[#t] if @scheme[v] is @scheme[#t] or @scheme[#f],
|
|
@scheme[#f] otherwise.
|
|
|
|
@examples[
|
|
(boolean? #f)
|
|
(boolean? #t)
|
|
(boolean? 'true)
|
|
]}
|
|
|
|
|
|
@defproc[(not [v any/c]) boolean?]{
|
|
|
|
Returns @scheme[#t] if @scheme[v] is @scheme[#f], @scheme[#f] otherwise.
|
|
|
|
@examples[
|
|
(not #f)
|
|
(not #t)
|
|
(not 'we-have-no-bananas)
|
|
]}
|
|
|
|
|
|
@defproc[(equal? [v1 any/c] [v2 any/c]) boolean?]{
|
|
|
|
Two values are @scheme[equal?] if and only if they are @scheme[eqv?],
|
|
unless otherwise specified for a particular datatype.
|
|
|
|
Datatypes with further specification of @scheme[equal?] include
|
|
strings, byte strings, numbers, pairs, mutable pairs, vectors, hash
|
|
tables, and inspectable structures. In the last five cases, equality
|
|
is recursively defined; if both @scheme[v1] and @scheme[v2] contain
|
|
reference cycles, they are equal when the infinite unfoldings of the
|
|
values would be equal. See also @scheme[prop:equal+hash].
|
|
|
|
@examples[
|
|
(equal? 'yes 'yes)
|
|
(equal? 'yes 'no)
|
|
(equal? (expt 2 100) (expt 2 100))
|
|
(equal? 2 2.0)
|
|
(equal? (make-string 3 #\z) (make-string 3 #\z))
|
|
]}
|
|
|
|
|
|
@defproc[(eqv? [v1 any/c] [v2 any/c]) boolean?]{
|
|
|
|
Two values are @scheme[eqv?] if and only if they are @scheme[eq?],
|
|
unless otherwise specified for a particular datatype.
|
|
|
|
The @tech{number} and @tech{character} datatypes are the only ones for which
|
|
@scheme[eqv?] differs from @scheme[eq?].
|
|
|
|
@examples[
|
|
(eqv? 'yes 'yes)
|
|
(eqv? 'yes 'no)
|
|
(eqv? (expt 2 100) (expt 2 100))
|
|
(eqv? 2 2.0)
|
|
(eqv? (integer->char #x3BB) (integer->char #x3BB))
|
|
(eqv? (make-string 3 #\z) (make-string 3 #\z))
|
|
]}
|
|
|
|
|
|
@defproc[(eq? [v1 any/c] [v2 any/c]) boolean?]{
|
|
|
|
Return @scheme[#t] if @scheme[v1] and @scheme[v2] refer to the same
|
|
object, @scheme[#f] otherwise. See also @secref["model-eq"].
|
|
|
|
@examples[
|
|
(eq? 'yes 'yes)
|
|
(eq? 'yes 'no)
|
|
(let ([v (mcons 1 2)]) (eq? v v))
|
|
(eq? (mcons 1 2) (mcons 1 2))
|
|
(eq? (make-string 3 #\z) (make-string 3 #\z))
|
|
]}
|
|
|
|
|
|
@defproc[(equal?/recur [v1 any/c] [v2 any/c] [recur-proc (any/c any/c -> any/c)]) boolean?]{
|
|
|
|
Like @scheme[equal?], but using @scheme[recur-proc] for recursive
|
|
comparisons (which means that reference cycles are not handled
|
|
automatically). Non-@scheme[#f] results from @scheme[recur-proc] are
|
|
converted to @scheme[#t] before being returned by
|
|
@scheme[equal?/recur].
|
|
|
|
@examples[
|
|
(equal?/recur 1 1 (lambda (a b) #f))
|
|
(equal?/recur '(1) '(1) (lambda (a b) #f))
|
|
(equal?/recur '#(1 1 1) '#(1 1.2 3/4)
|
|
(lambda (a b) (<= (abs (- a b)) 0.25)))
|
|
]}
|
|
|
|
|
|
@defproc[(immutable? [v any/c]) boolean?]{
|
|
|
|
Returns @scheme[#t] if @scheme[v] is an immutable @tech{string},
|
|
@tech{byte string}, @tech{vector}, @tech{hash table}, or box,
|
|
@scheme[#f] otherwise.}
|
|
|
|
@defthing[prop:equal+hash struct-type-property?]{
|
|
|
|
A @tech{structure type property} (see @secref["structprops"]) that
|
|
supplies an equality predicate and hashing functions for a structure
|
|
type. The property value must be a list of three procedures:
|
|
|
|
@itemize{
|
|
|
|
@item{@scheme[_equal-proc : (any/c any/c (any/c any/c . ->
|
|
. boolean?) . -> . any/c)] --- tests whether the first two
|
|
arguments are equal, where both values are instances of the
|
|
structure type to which the property is associated (or a
|
|
subtype of the structure type).
|
|
|
|
The third argument is an @scheme[equal?] predicate to use for
|
|
recursive equality checks; use the given predicate instead of
|
|
@scheme[equal?] to ensure that data cycles are handled
|
|
properly and to work with @scheme[equal?/recur] (but beware
|
|
that an arbitrary function can be provided to
|
|
@scheme[equal?/recur] for recursive checks, which means that
|
|
arguments provided to the predicate might be exposed to
|
|
arbitrary code).
|
|
|
|
The @scheme[_equal-proc] is called for a pair of structures
|
|
only when they are not @scheme[eq?], and only when they both
|
|
have a @scheme[prop:equal+hash] value inherited from the same
|
|
structure type. With this strategy, the order in which
|
|
@scheme[equal?] receives two structures does not matter. It
|
|
also means that, by default, a structure sub-type inherits the
|
|
equality predicate of its parent, if any.}
|
|
|
|
@item{@scheme[_hash-proc : (any/c (any/c . -> . exact-integer?) . ->
|
|
. exact-integer?)] --- computes a hash code for the given
|
|
structure, like @scheme[equal-hash-code]. The first argument is
|
|
an instance of the structure type (or one of its subtypes) to
|
|
which the property is associated.
|
|
|
|
The second argument is a @scheme[equal-hash-code]-like
|
|
procedure to use for recursive hash-code computation; use the
|
|
given procedure instead of @scheme[equal-hash-code] to ensure
|
|
that data cycles are handled properly.}
|
|
|
|
@item{@scheme[_hash2-proc : (any/c (any/c . -> . exact-integer?) . ->
|
|
. exact-integer?)] --- computes a secondary hash code for the
|
|
given structure. This procedure is like @scheme[_hash-proc],
|
|
but analogous to @scheme[equal-secondary-hash-code].}
|
|
|
|
}
|
|
|
|
Take care to ensure that @scheme[_hash-proc] and @scheme[_hash2-proc]
|
|
are consistent with @scheme[_equal-proc]. Specifically,
|
|
@scheme[_hash-proc] and @scheme[_hash2-proc] should produce the same
|
|
value for any two structures for which @scheme[_equal-proc] produces a
|
|
true value.
|
|
|
|
When a structure type has no @scheme[prop:equal+hash] property, then
|
|
transparent structures (i.e., structures with an @tech{inspector} that
|
|
is controlled by the current @tech{inspector}) are @scheme[equal?]
|
|
when they are instances of the same structure type (not counting
|
|
sub-types), and when they have @scheme[equal?] field values. For
|
|
transparent structures, @scheme[equal-hash-code] and
|
|
@scheme[equal-secondary-hash-code] derive hash code using the field
|
|
values. For opaque structure types, @scheme[equal?] is the same as
|
|
@scheme[eq?], and @scheme[equal-hash-code] and
|
|
@scheme[equal-secondary-hash-code] results are based only on
|
|
@scheme[eq-hash-code].}
|
|
|
|
@section{Boolean Synonyms}
|
|
|
|
@note-lib[scheme/bool]
|
|
|
|
@defthing[true boolean?]{A synonym for @scheme[#t].}
|
|
|
|
@defthing[false boolean?]{A synonym for @scheme[#f].}
|
|
|
|
@defproc[(symbol=? [a symbol?] [b symbol?]) boolean?]{
|
|
|
|
Returns @scheme[(equal? a b)].}
|
|
|
|
@defproc[(boolean=? [a boolean?] [b boolean?]) boolean?]{
|
|
|
|
Returns @scheme[(equal? a b)].}
|
|
|
|
@defproc[(false? [v any/c]) boolean?]{
|
|
|
|
Returns @scheme[(not v)].}
|