Predicates
1 Logic Predicate Constructors
and?
or?
not?
and?*
or?*
2 Comparison Predicate Constructors
eq??
eqv??
equal??
=?
<?
>?
<=?
>=?
3 List Predicates
not-null?
nonempty-list?
nonsingular-list?
length>?
length=?
length<?
first?
second?
third?
fourth?
rest?
all?
listof?
list-with-head?
4 Conditional Combinators
if?
when?
unless?
while?
until?
do-while?
do-until?
5 Miscellaneous
true?
without-truthiness
in-range?
8.12

Predicates🔗ℹ

 (require predicates) package: 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🔗ℹ

procedure

(and? pred ...+)  (-> any? boolean?)

  pred : (-> any? boolean?)
Combines each pred into a single predicate that returns #t for its input 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

procedure

(or? pred ...+)  (-> any? boolean?)

  pred : (-> any? boolean?)
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

procedure

(not? pred)  (-> any? boolean?)

  pred : (-> any? boolean?)
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

procedure

(and?* pred ...+)  (->* () () #:rest any? boolean?)

  pred : (-> any? boolean?)
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

procedure

(or?* pred ...+)  (->* () () #:rest any? boolean?)

  pred : (-> any? boolean?)
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🔗ℹ

procedure

(eq?? v)  (-> any? boolean?)

  v : any?
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

procedure

(eqv?? v)  (-> any? boolean?)

  v : any?
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

procedure

(equal?? v)  (-> any? boolean?)

  v : any?
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

procedure

(=? v)  (-> any? boolean?)

  v : any?
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

procedure

(<? v)  (-> real? boolean?)

  v : real?
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

procedure

(>? v)  (-> real? boolean?)

  v : real?
Returns a predicate that returns #t for any real input that is > than v.

Examples:
> (define >10? (>? 10))
> (>10? 15)

#t

> (>10? 5)

#f

procedure

(<=? v)  (-> real? boolean?)

  v : real?
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

procedure

(>=? v)  (-> real? boolean?)

  v : real?
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🔗ℹ

procedure

(not-null? v)  boolean?

  v : any?
Returns #t if v is null?, and returns #f otherwise. Equivalent to (not? null?).

Examples:
> (not-null? null)

#f

> (not-null? '())

#f

> (not-null? 'foo)

#t

procedure

(nonempty-list? v)  boolean?

  v : any?
Returns #t if v is a list containing at least one element, and returns #f otherwise. Equivalent to (and? list? (not? null?)).

Examples:
> (nonempty-list? '(foo bar baz))

#t

> (nonempty-list? null)

#f

> (nonempty-list? '())

#f

> (nonempty-list? 'foo)

#f

procedure

(nonsingular-list? v)  boolean?

  v : any?
Returns #t if v is a list containing at least two elements, and returns #f otherwise. Equivalent to (and? list? (not? null?) (rest? (not? null?))).

Examples:
> (nonsingular-list? '(foo bar baz))

#t

> (nonsingular-list? '(foo))

#f

> (nonsingular-list? '())

#f

> (nonsingular-list? null)

#f

> (nonsingular-list? 7)

#f

procedure

(length>? n)  (-> list? boolean?)

  n : exact-nonnegative-integer?
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

procedure

(length=? n)  (-> list? boolean?)

  n : exact-nonnegative-integer?
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

procedure

(length<? n)  (-> list? boolean?)

  n : exact-nonnegative-integer?
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

procedure

(first? pred ...+)  (-> nonempty-list? boolean?)

  pred : (-> any? boolean?)

procedure

(second? pred ...+)  (-> (and? list? (length>? 1)) boolean?)

  pred : (-> any? boolean?)

procedure

(third? pred ...+)  (-> (and? list? (length>? 2)) boolean?)

  pred : (-> any? boolean?)

procedure

(fourth? pred ...+)  (-> (and? list? (length>? 3)) boolean?)

  pred : (-> any? boolean?)
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

procedure

(rest? pred)  (-> list? boolean?)

  pred : (-> any? boolean?)
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

procedure

(all? pred)  (-> list? boolean?)

  pred : (-> any? boolean?)
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

procedure

(listof? pred ...+)  (-> any? boolean?)

  pred : (-> any? boolean?)
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

procedure

(list-with-head? pred ...+)  (-> any? boolean?)

  pred : (-> any? boolean?)
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🔗ℹ

procedure

(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)).

Examples:
> (define abs-add1 (if? positive? add1 sub1))
> (abs-add1 4)

5

> (abs-add1 -4)

-5

> (abs-add1 0)

-1

procedure

(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

procedure

(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)

procedure

(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:
> (define while-negative-add10
    (while? (<? 0) (λ (x) (+ x 10))))
> (while-negative-add10 -7)

3

> (while-negative-add10 -23)

7

> (while-negative-add10 15)

15

procedure

(until? pred f)  (-> any? any?)

  pred : (-> any? boolean?)
  f : (-> any? any?)
Logical inverse of while?. Equivalent to (while? (not? p) f).

Examples:
> (define until-negative-sub10
    (until? (<? 0) (λ (x) (- x 10))))
> (until-negative-sub10 7)

-3

> (until-negative-sub10 23)

-7

> (until-negative-sub10 -15)

-15

procedure

(do-while? pred f)  (-> any? any?)

  pred : (-> any? boolean?)
  f : (-> any? any?)
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:
> (define do-while-negative-add10
    (do-while? (<? 0) (λ (x) (+ x 10))))
> (do-while-negative-add10 -7)

3

> (do-while-negative-add10 -23)

7

> (do-while-negative-add10 15)

25

procedure

(do-until? pred f)  (-> any? any?)

  pred : (-> any? boolean?)
  f : (-> any? any?)
Logical inverse of do-while?. Equivalent to (do-while? (not? p) f).

Examples:
> (define do-until-negative-sub10
    (do-until? (<? 0) (λ (x) (- x 10))))
> (do-until-negative-sub10 7)

-3

> (do-until-negative-sub10 23)

-7

> (do-until-negative-sub10 -15)

-25

5 Miscellaneous🔗ℹ

procedure

(true? v)  boolean?

  v : any?
Returns #t if v is not #f, and #f otherwise. Useful to turn "truthy" functions into predicates that only return #t or #f.

Examples:
> (true? #t)

#t

> (true? #f)

#f

> (true? 'foo)

#t

procedure

(without-truthiness f)  proc?

  f : proc?
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.

Examples:
> (define member? (without-truthiness member))
> (member? 1 '(1 2 3))

#t

> (member? 'foo '(1 2 3))

#f

procedure

(in-range? low high [exclusive?])  (-> any? boolean?)

  low : real?
  high : real?
  exclusive? : boolean? = #f
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