2 Functions
(require (planet cce/scheme:4:1/function)) |
This module provides tools for higher-order programming and creating functions.
2.1 Simple Functions
(identity x) → (one-of/c x) |
x : any/c |
Returns x.
(constant x) → (unconstrained-> (one-of/c x)) |
x : any/c |
Produces a function that returns x regardless of input.
Examples: | ||
| ||
> (f) | ||
g5027 | ||
> (f '(4 5 6)) | ||
g5027 | ||
> (f #:x 7 #:y 8 #:z 9) | ||
g5027 |
(thunk body ) |
Creates a function that ignores its inputs and evaluates the given body. Useful for creating event handlers with no (or irrelevant) arguments.
Examples: | ||
| ||
> (f) | ||
1 | ||
> (f 'x) | ||
1 | ||
> (f #:y 'z) | ||
1 |
2.2 Higher Order Predicates
((conjoin f ) x ) → boolean? |
f : (-> A ... boolean?) |
x : A |
Combines calls to each function with and. Equivalent to (and (f x ...) ...)
Examples: | ||
| ||
> (f 1) | ||
#t | ||
> (f 1.0) | ||
#f | ||
> (f 1/2) | ||
#f | ||
> (f 0.5) | ||
#f |
((disjoin f ) x ) → boolean? |
f : (-> A ... boolean?) |
x : A |
Combines calls to each function with or. Equivalent to (or (f x ...) ...)
Examples: | ||
| ||
> (f 1) | ||
#t | ||
> (f 1.0) | ||
#t | ||
> (f 1/2) | ||
#t | ||
> (f 0.5) | ||
#f |
2.3 Currying and (Partial) Application
(call f x ) → B |
f : (-> A ... B) |
x : A |
Passes x ... to f. Keyword arguments are allowed. Equivalent to (f x ...). Useful for application in higher-order contexts.
Examples: | ||||
| ||||
(6 -4 21 1/2) | ||||
| ||||
| ||||
| ||||
| ||||
> (for-each call (list inc inc show reset show)) | ||||
|
| |||
|
The papply and papplyr functions partially apply f to x ..., which may include keyword arguments. They obey the following equations:
((papply f x ...) y ...) = (f x ... y ...) |
((papplyr f x ...) y ...) = (f y ... x ...) |
Examples: | ||
| ||
> (reciprocal 3) | ||
1/3 | ||
> (reciprocal 4) | ||
1/4 | ||
| ||
> (halve 3) | ||
3/2 | ||
> (halve 4) | ||
2 |
| ||||
|
Note: The ooo above denotes a loosely associating ellipsis.
The curryn and currynr functions construct a curried version of f, specialized at x ..., that produces a result after n further applications. Arguments at any stage of application may include keyword arguments, so long as no keyword is duplicated. These curried functions obey the following equations:
(curryn 0 f x ...) = (f x ...) |
((curryn (+ n 1) f x ...) y ...) = (curryn n f x ... y ...) |
(currynr 0 f x ...) = (f x ...) |
((currynr (+ n 1) f x ...) y ...) = (currynr n f y ... x ...) |
The call, papply, and papplyr utilities are related to curryn and currynr in the following manner:
(call f x ...) = (curryn 0 f x ...) = (currynr 0 f x ...) |
(papply f x ...) = (curryn 1 f x ...) |
(papplyr f x ...) = (currynr 1 f x ...) |
Examples: | ||
| ||
> (reciprocal 3) | ||
1/3 | ||
> (reciprocal 4) | ||
1/4 | ||
| ||
| ||
> (from-10 5) | ||
5 | ||
> (from-10 10) | ||
0 | ||
| ||
> (from-0 5) | ||
-5 | ||
> (from-0 10) | ||
-10 | ||
| ||
> (halve 3) | ||
3/2 | ||
> (halve 4) | ||
2 | ||
| ||
| ||
> (minus-10 5) | ||
-5 | ||
> (minus-10 10) | ||
0 | ||
| ||
> (minus-0 5) | ||
5 | ||
> (minus-0 10) | ||
10 |
2.4 Parameter Arguments
(lambda/parameter (param-arg ) body ) | ||||||||||||||||||||||||||||||
|
Constructs a function much like lambda, except that some optional arguments correspond to the value of a parameter. For each clause of the form [id #:param param-expr], param-expr must evaluate to a value param satisfying parameter?. The default value of the argument id is (param); param is bound to id via parameterize during the function call.
Examples: | |||||
| |||||
| |||||
> (hello-world p) | |||||
> (get-output-string p) | |||||
"Hello, World!\n" |