6.3.90.900
Predicates
These functions allow for easy construction of predicates - functions that take
an input and return a boolean - for use with contracts and filter variants. This
library makes it easy to define predicates in a point-free style, meaning that
you can construct new predicates in terms of old predicates without defining them as
functions with arguments.
source code: https://github.com/jackfirth/predicates
1 Logic Predicate Constructors
Combines each pred into a single predicate that returns #t for its
if all the original preds return #t for the input.
Examples:
> (define small-positive-number? (and? number? (λ (x) (< 0 x 10)))) |
|
> (small-positive-number? 6) |
#t |
> (small-positive-number? 123) |
#f |
> (small-positive-number? 'foo) |
#f |
Combines each pred into a single predicate that returns #t for its
input if any of the original preds return #t for the input.
Examples:
> (define symbol-or-string? (or? symbol? string?)) |
|
> (symbol-or-string? 'symbol) |
#t |
> (symbol-or-string? "string") |
#t |
> (symbol-or-string? 123) |
#f |
Returns a new predicate that returns #t for its input if the original
predicate returns #f for the input.
Examples:
> (define nonzero? (not? zero?)) |
|
> (nonzero? 8) |
#t |
> (nonzero? 0) |
#f |
> (nonzero? -5) |
#t |
Combines each pred into a single function that accepts any number of
arguments and returns #t if the first pred returns #t
for the first argument, and the second pred returns #t for the
second argument, and so on for each pred and each argument.
Examples:
> (define number-and-string? (and?* number? string?)) |
|
> (number-and-string? 5 "foo") |
#t |
> (number-and-string? 5 'neither) |
#f |
> (number-and-string? 'neither "foo") |
#f |
> (number-and-string? 'neither 'neither) |
#f |
Combines each
pred into a single function in a manner similar to
and?*, except the resulting function returns
#t if any of
the
preds return
#t for their argument.
Examples:
> (define number-or-string? (or?* number? string?)) |
|
> (number-or-string? 5 "foo") |
#t |
> (number-or-string? 5 'neither) |
#t |
> (number-or-string? 'neither "foo") |
#t |
> (number-or-string? 'neither 'neither) |
#f |
2 Comparison Predicate Constructors
Returns a predicate that returns
#t for any input that is
eq?
to
v.
Examples:
> (define eq-foo? (eq?? 'foo)) |
|
> (eq-foo? 'foo) |
#t |
> (eq-foo? 8) |
#f |
> (eq-foo? 'bar) |
#f |
Returns a predicate that returns
#t for any input that is
eqv?
to
v.
Examples:
> (define eqv-7? (eqv?? 7)) |
|
> (eqv-7? 7) |
#t |
> (eqv-7? 8) |
#f |
> (eqv-7? 'foo) |
#f |
Returns a predicate that returns
#t for any input that is
equal?
to
v.
Examples:
> (define foo-bar-baz? (equal?? '(foo bar baz))) |
|
> (foo-bar-baz? '(foo bar baz)) |
#t |
> (foo-bar-baz? '(foo foo foo)) |
#f |
> (foo-bar-baz? 8) |
#f |
Returns a predicate that returns
#t for any input that is
=
to
v.
Examples:
> (define 7? (=? 7)) |
|
> (7? 7) |
#t |
> (7? (+ 3 4)) |
#t |
> (7? 0) |
#f |
Returns a predicate that returns
#t for any real input that is
<
than
v.
Examples:
> (define <10? (<? 10)) |
|
> (<10? 5) |
#t |
> (<10? 15) |
#f |
> (<10? -5) |
#t |
Returns a predicate that returns
#t for any real input that is
>
than
v.
Examples:
> (define >10? (>? 10)) |
|
> (>10? 15) |
#t |
> (>10? 5) |
#f |
Returns a predicate that returns
#t for any real input that is
<=
to
v.
Examples:
> (define <=10? (<=? 10)) |
|
> (<=10? 10) |
#t |
> (<=10? 5) |
#t |
> (<=10? 15) |
#f |
> (<=10? -5) |
#t |
Returns a predicate that returns
#t for any real input that is
>=
to
v.
Examples:
> (define >=10? (>=? 10)) |
|
> (>=10? 10) |
#t |
> (>=10? 15) |
#t |
> (>=10? 5) |
#f |
3 List Predicates
Returns
#t if
v is
null?, and returns
#f otherwise.
Equivalent to
(not? null?).
Returns
#t if
v is a list containing at least one element, and returns
#f otherwise. Equivalent to
(and? list? (not? null?)).
Returns a predicate that returns #t for any list with more than n
elements.
Examples:
> (define more-than-four-elements? (length>? 4)) |
|
> (more-than-four-elements? '(foo bar baz bar foo)) |
#t |
> (more-than-four-elements? '(foo bar baz bar)) |
#f |
> (more-than-four-elements? '()) |
#f |
> (more-than-four-elements? null) |
#f |
Returns a predicate that returns #t for any list with n elements.
Examples:
> (define four-element-list? (length=? 4)) |
|
> (four-element-list? '(foo bar baz bar)) |
#t |
> (four-element-list? '(foo bar baz bar foo)) |
#f |
> (four-element-list? '(foo bar baz)) |
#f |
> (four-element-list? '()) |
#f |
Returns a predicate that returns #t for any list with fewer than n
elements.
Examples:
> (define less-than-four-elements? (length<? 4)) |
|
> (less-than-four-elements? '(foo bar baz)) |
#t |
> (less-than-four-elements? '(foo bar baz bar)) |
#f |
> (less-than-four-elements? '()) |
#t |
Returns a predicate that returns
#t for any list whose first, second, third,
or fourth item satisfies
(and? pred ...), depending on the procedure chosen.
Examples:
> (define second-is-number? (second? number?)) |
|
> (second-is-number? '(foo 4 bar baz)) |
#t |
> (second-is-number? '(foo bar baz)) |
#f |
> (second-is-number? '(5 5)) |
#t |
Returns a predicate that returns
#t for any list for which the
rest
of the list satisfies
pred.
Examples:
> (define rest-numbers? (rest? (all? number?))) |
|
> (rest-numbers? '(foo 1 2 3)) |
#t |
> (rest-numbers? '(foo 1)) |
#t |
> (rest-numbers? '(foo)) |
#t |
> (rest-numbers? '(foo bar baz)) |
#f |
> (rest-numbers? '(foo bar 1)) |
#f |
Returns a predicate that returns #t for any list for which every element in
the list satisfies pred.
Examples:
> (define all-numbers? (all? number?)) |
|
> (all-numbers? '(1 2 3 4)) |
#t |
> (all-numbers? '(1 2 foo 4)) |
#f |
> (all-numbers? '()) |
#t |
Returns a predicate that returns #t for any value that is a list with one
element for each pred whose first element satisfies the first pred,
second element satisfies the second pred, and so on for each pred.
Examples:
> (define num-sym-num? (listof? number? symbol? number?)) |
|
> (num-sym-num? '(1 foo 2)) |
#t |
> (num-sym-num? '(1 2 3)) |
#f |
> (num-sym-num? '(foo bar baz)) |
#f |
> (num-sym-num? '(1 foo)) |
#f |
> (num-sym-num? '(1 foo 2 bar)) |
#f |
> (num-sym-num? 'foo) |
#f |
Similar to listof? but returns #t for lists with extra elements
Examples:
> (define starts-with-num-sym? (list-with-head? number? symbol?)) |
|
> (starts-with-num-sym? '(1 foo 2 3 4 5)) |
#t |
> (starts-with-num-sym? '(1 foo)) |
#t |
> (starts-with-num-sym? '(foo bar baz)) |
#f |
> (starts-with-num-sym? '(1 2 3)) |
#f |
> (starts-with-num-sym? '()) |
#f |
> (starts-with-num-sym? 5) |
#f |
4 Conditional Combinators
(if? pred f [g]) → (-> any? any?)
|
pred : (-> any? boolean?) |
f : (-> any? any?) |
g : (-> any? any?) = identity |
Returns a function that, for an input
v, returns
(if (pred v) (f v) (g v)).
(when? pred f) → (-> any? any?)
|
pred : (-> any? boolean?) |
f : (-> any? any?) |
Returns a function that, for an input
v, returns
(when (pred v) (f v)).
Examples:
> (define displayln-when-even? (when? even? displayln)) |
|
> (displayln-when-even? 5) |
|
> (displayln-when-even? 4) |
4 |
|
> (displayln-when-even? 10) |
10 |
|
(unless? pred f) → (-> any? any?)
|
pred : (-> any? boolean?) |
f : (-> any? any?) |
Returns a function that, for an input
v, returns
(unless (pred v) (f v)).
Examples:
> (define displayln-unless-even? (unless? even? displayln)) |
|
> (displayln-unless-even? 5) |
5 |
|
> (displayln-unless-even? 4) |
|
> (displayln-unless-even? 10) |
|
(while? pred f) → (-> any? any?)
|
pred : (-> any? boolean?) |
f : (-> any? any?) |
Returns a function that, for an input v, returns just v if (p v)
is false, otherwise it recursively calls itself with (f v).
Examples:
|
|
> (while-negative-add10 -7) |
3 |
> (while-negative-add10 -23) |
7 |
> (while-negative-add10 15) |
15 |
(until? pred f) → (-> any? any?)
|
pred : (-> any? boolean?) |
f : (-> any? any?) |
Examples:
|
|
> (until-negative-sub10 7) |
-3 |
> (until-negative-sub10 23) |
-7 |
> (until-negative-sub10 -15) |
-15 |
Like
while? except that
f is guaranteed to be called at least once.
Similar to the relationship between
while and
do ... while loops in
imperative languages. Equivalent to
(compose (while? p f) f).
Examples:
|
|
> (do-while-negative-add10 -7) |
3 |
> (do-while-negative-add10 -23) |
7 |
> (do-while-negative-add10 15) |
25 |
Examples:
|
|
> (do-until-negative-sub10 7) |
-3 |
> (do-until-negative-sub10 23) |
-7 |
> (do-until-negative-sub10 -15) |
-25 |
5 Miscellaneous
Returns #t if v is not #f, and #f otherwise.
Useful to turn "truthy" functions into predicates that only return #t
or #f.
Returns a procedure that’s like f, but returns either #t or
#f based on whether f returns false. Essentially, this procedure
turns functions that rely on returning "truthy" values into function that only
return a boolean.
Returns a predicate that determins in its input is a real number between
low
and
high. If
exclusive? is
#t, then values
= to
low or
high will return
#f.
Examples:
> (define zero-to-ten? (in-range? 0 10)) |
|
> (zero-to-ten? 5) |
#t |
> (zero-to-ten? 0) |
#t |
> (zero-to-ten? 10) |
#t |
> (zero-to-ten? 15) |
#f |
> (zero-to-ten? -100) |
#f |
> (define between-zero-and-ten? (in-range? 0 10 #t)) |
|
> (between-zero-and-ten? 5) |
#t |
> (between-zero-and-ten? 0) |
#f |
> (between-zero-and-ten? 10) |
#f |