2 Utilities for Contracts
Gets the contract-name
of the given value if it’s a contract, and merely returns the value otherwise. This can be handy when defining a new contract where the contract-name
may embed arbitrary values, but embedded contracts in particular should be easy to read.
(let/c [var-id val-expr] ... body-expr)
Evaluates each val-expr
, evaluates the body-expr
with those values in scope under respective var-id
variables, and renames the resulting contract to `(let/c [var-id ,(value-name-for-contract val)] ... ,body-expr)
, where each val
is the result of a val-expr
This can come in handy when composiing relatively large contracts that use the same value in multiple places. It keeps the name more concise than it usually would be.
(fix/c self-id options ... contract-expr)
|(fix/c (self-id [arg-id arg-expr] ...) options ... contract-expr)|
A fixed-point syntax for contracts. Returns the result of running contract-expr with a certain contract or contract-returning function in scope as self-id, and with each given arg-expr’s result in scope as the corresponding arg-id.
In the unparenthesized case, self-id is bound to a contract. The contract functionality of self-id should be used only after contract-expr has returned a contract, and it behaves just like that contract.
In the parenthesized case, self-id is bound to a contract-returning function that takes one argument for each arg-id. The contract functionality of the result of self-id should be used only after contract-expr has returned a contract, and it works by evaluating contract-expr again with arg-id bound to each function argument.
In both cases, the contract obtained from self-id is delayed so that it can be used without causing an infinite loop. This functionality is based on recursive-contract, and the given options supply the optional arguments of the recursive-contract operation.
#<contract: (fix/c simple-s-expression/c (or/c symbol? (listof simple-s-expression/c)))>
A syntax for contracts that depend on the value they apply to. Returns a contract that tries to match the value against the match pattern pat, and if successful, executes the body-expr (with all the bound variables from pat in scope) and behaves according to that contract. If the match is not successful, then the value is not considered to meet this contract.
The name of the returned contract includes pat and body-expr verbatim. If they contain references to variables defined elsewhere, let/c may be useful to ensure those variable bindings are apparent in the overall contract name.
Returns a contract that recognizes a value if the given value is equal?
Returns a flat contract that recognizes any flat contract that recognizes the given value.