▼ |  | Unstable: May Change Without Warning

1 | Guidelines for Developing unstable Libraries
2 | Automata: Compiling State Machines
3 | Bytes
4 | Contracts
5 | Contracts for Macro Subexpressions
6 | Debugging
7 | Definitions
8 | Errors
9 | Filesystem
10 | Find
11 | Flonums
12 | Futures
13 | Functions
14 | Hash Tables
15 | Interface-Oriented Programming for Classes
16 | Lazy Require
17 | Lists
18 | Logging
19 | Macro Testing
20 | Mark Parameters
21 | Match
22 | Open place expressions
23 | Options
24 | Parameter Groups
25 | Pretty-Printing
26 | Re-Contracting Identifiers
27 | Sandbox
28 | Sequences
29 | Strings
30 | Structs
31 | Struct Printing
32 | Syntax
33 | Temporal Contracts: Explicit Contract Monitors
34 | Unix Domain Sockets
35 | 2D Syntax
36 | GUI Libraries

On this page:

non-empty-string?
port-number?
tcp-listen-port?
path-piece?
if/ c
failure-result/ c
rename-contract
maybe/ c
truth/ c
sequence/ c
treeof

5.3.6
top ← prev up next →

### 4`` Contracts

This library is unstable; compatibility will not be maintained. See Unstable: May Change Without Warning for more information.

(require unstable/contract)

> > procedure

> > (non-empty-string? x) → boolean?

> x : any/c

Returns #t if x is a string and is not empty; returns #f otherwise.

> > value

> > port-number? : contract?

Equivalent to (between/c 1 65535).

> > value

> > tcp-listen-port? : contract?

Equivalent to (between/c 0 65535).

> > value

> > path-piece? : contract?

Equivalent to (or/c path-string? (symbols 'up 'same)).

> > > The subsequent bindings were added by Ryan Culpepper.

> > procedure

> > (if/c predicate then-contract else-contract) → contract?

> predicate : (-> any/c any/c)
> then-contract : contract?
> else-contract : contract?

Produces a contract that, when applied to a value, first tests the value with predicate; if predicate returns true, the then-contract is applied; otherwise, the else-contract is applied. The resulting contract is a flat contract if both then-contract and else-contract are flat contracts.

For example, the following contract enforces that if a value is a procedure, it is a thunk; otherwise it can be any (non-procedure) value:

> (if/c procedure? (-> any) any/c)

Note that the following contract is not equivalent:

> (or/c (-> any) any/c) ; wrong!

The last contract is the same as any/c because or/c tries flat contracts before higher-order contracts.

> > value

> > failure-result/c : contract?

A contract that describes the failure result arguments of procedures such as hash-ref.

Equivalent to (if/c procedure? (-> any) any/c).

> > procedure

> > (rename-contract contract name) → contract?

> contract : contract?
> name : any/c

Produces a contract that acts like contract but with the name name.

The resulting contract is a flat contract if contract is a flat contract.

> > > The subsequent bindings were added by Asumu Takikawa.

> > procedure

> > (maybe/c contract) → contract?

> contract : contract?

Creates a contract that acts like contract but will also accept #f. Intended to describe situations where a failure or default value may be used.

> > > The subsequent bindings were added by Carl Eastlund <cce@racket-lang.org>.

> > value

> > truth/c : flat-contract?

This contract recognizes Scheme truth values, i.e., any value, but with a more informative name and description. Use it in negative positions for arguments that accept arbitrary truth values that may not be booleans.

> > procedure

> > (sequence/c elem/c ...) → contract?

> elem/c : contract?

Wraps a sequence, obligating it to produce as many values as there are elem/c contracts, and obligating each value to satisfy the corresponding elem/c. The result is not guaranteed to be the same kind of sequence as the original value; for instance, a wrapped list is not guaranteed to satisfy list?.

Examples:

> > (define/contract predicates
> (sequence/c (-> any/c boolean?))
> (in-list (list integer?
> string->symbol)))

> > (for ([P predicates])
> (printf "~s\n" (P "cat")))

> #f
> predicates: broke its contract
> promised: boolean?
> produced: 'cat
> in: the range of
> ...
> ...
> (sequence/c predicate/c)
> contract from: (definition predicates)
> blaming: (definition predicates)
> at: eval:2.0

> > (define/contract numbers&strings
> (sequence/c number? string?)
> (in-dict (list (cons 1 "one")
> (cons 2 "two")
> (cons 3 'three))))

> > (for ([(N S) numbers&strings])
> (printf "~s: ~a\n" N S))

> 1: one
> 2: two

> numbers&strings: broke its contract
> promised: string?
> produced: 'three
> in: (sequence/c number? string?)
> contract from: (definition numbers&strings)
> blaming: (definition numbers&strings)
> at: eval:4.0

> > > The subsequent bindings were added by Neil Toronto <neil.toronto@gmail.com>.

> > procedure

> > (treeof elem-contract) → contract?

> elem-contract : contract?

Identifies values that meet the contract elem-contract, lists of such values, lists of lists, and so on.

Examples:

> > (define number-tree/c (treeof number?))

> > (flat-contract? number-tree/c)
> #t
> > (define number-tree? (flat-contract-predicate number-tree/c))

> > (number-tree? 4)
> #t
> > (number-tree? '(4 5))
> #t
> > (number-tree? '((4 5) 6))
> #t
> > (number-tree? '(4 . 5))
> #f

top ← prev up next →