3.1 Booleans and Equality
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.
See also: and, or, andmap, ormap.
Examples: |
> (boolean? #f) |
#t |
> (boolean? #t) |
#t |
> (boolean? 'true) |
#f |
Examples: |
> (not #f) |
#t |
> (not #t) |
#f |
> (not 'we-have-no-bananas) |
#f |
Datatypes with further specification of equal? include strings, byte strings, numbers, pairs, mutable pairs, vectors, boxes, hash tables, and inspectable structures. In the last five cases, equality is recursively defined; if both v1 and v2 contain reference cycles, they are equal when the infinite unfoldings of the values would be equal. See also prop:equal+hash and prop:impersonator-of.
Examples: |
> (equal? 'yes 'yes) |
#t |
> (equal? 'yes 'no) |
#f |
> (equal? (expt 2 100) (expt 2 100)) |
#t |
> (equal? 2 2.0) |
#f |
> (equal? (make-string 3 #\z) (make-string 3 #\z)) |
#t |
The number and character datatypes are the only ones for which eqv? differs from eq?.
Examples: |
> (eqv? 'yes 'yes) |
#t |
> (eqv? 'yes 'no) |
#f |
> (eqv? (expt 2 100) (expt 2 100)) |
#t |
> (eqv? 2 2.0) |
#f |
> (eqv? (integer->char 955) (integer->char 955)) |
#t |
> (eqv? (make-string 3 #\z) (make-string 3 #\z)) |
#f |
Examples: |
> (eq? 'yes 'yes) |
#t |
> (eq? 'yes 'no) |
#f |
> (let ([v (mcons 1 2)]) (eq? v v)) |
#t |
> (eq? (mcons 1 2) (mcons 1 2)) |
#f |
> (eq? (make-string 3 #\z) (make-string 3 #\z)) |
#f |
(equal?/recur v1 v2 recur-proc) → boolean? |
v1 : any/c |
v2 : any/c |
recur-proc : (any/c any/c -> any/c) |
Examples: | ||
> (equal?/recur 1 1 (lambda (a b) #f)) | ||
#t | ||
> (equal?/recur '(1) '(1) (lambda (a b) #f)) | ||
#f | ||
| ||
#t |
(immutable? v) → boolean? |
v : any/c |
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 |
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 equal? predicate to use for recursive equality checks; use the given predicate instead of equal? to ensure that data cycles are handled properly and to work with equal?/recur (but beware that an arbitrary function can be provided to equal?/recur for recursive checks, which means that arguments provided to the predicate might be exposed to arbitrary code).
The equal-proc is called for a pair of structures only when they are not eq?, and only when they both have a prop:equal+hash value inherited from the same structure type. With this strategy, the order in which 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.
hash-proc : (-> any/c (-> any/c exact-integer?) exact-integer?) —
computes a hash code for the given structure, like 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 an equal-hash-code-like procedure to use for recursive hash-code computation; use the given procedure instead of equal-hash-code to ensure that data cycles are handled properly.
hash2-proc : (-> any/c (-> any/c exact-integer?) exact-integer?) —
computes a secondary hash code for the given structure. This procedure is like hash-proc, but analogous to equal-secondary-hash-code.
Take care to ensure that hash-proc and hash2-proc are consistent with equal-proc. Specifically, hash-proc and hash2-proc should produce the same value for any two structures for which equal-proc produces a true value.
When a structure type has no prop:equal+hash property, then transparent structures (i.e., structures with an inspector that is controlled by the current inspector) are equal? when they are instances of the same structure type (not counting sub-types), and when they have equal? field values. For transparent structures, equal-hash-code and equal-secondary-hash-code derive hash code using the field values. For opaque structure types, equal? is the same as eq?, and equal-hash-code and equal-secondary-hash-code results are based only on eq-hash-code. If a structure has a prop:impersonator-of property, then the prop:impersonator-of property takes precedence over prop:equal+hash if the property value’s procedure returns a non-#f value when applied to the structure.
Examples: | |||||||
| |||||||
| |||||||
| |||||||
| |||||||
> (define east (make-farm 5 2 20)) | |||||||
> (define west (make-farm 18 6 14)) | |||||||
> (define north (make-farm 5 20 20)) | |||||||
> (define south (make-farm 18 6 14)) | |||||||
> (equal? east west) | |||||||
#f | |||||||
> (equal? east north) | |||||||
#f | |||||||
> (equal? west south) | |||||||
#t |