1 Functions
(require (planet cce/scheme:6:1/function)) |
This module provides tools for higher-order programming and creating functions.
1.1 Simple Functions
(thunk body ...) |
Examples: | ||
| ||
> (f) | ||
1 | ||
> (f 'x) | ||
1 | ||
> (f #:y 'z) | ||
1 |
(constant x) → (unconstrained-domain-> (one-of/c x)) |
x : any/c |
(const x) → (unconstrained-domain-> (one-of/c x)) |
x : any/c |
This function is reprovided from scheme/function. In versions of PLT Scheme before const was implemented, this module provides its own definition.
Examples: | ||
| ||
> (f) | ||
5 | ||
> (f 'x) | ||
5 | ||
> (f #:y 'z) | ||
5 |
1.2 Higher Order Predicates
This function is reprovided from scheme/function.
Examples: | ||
| ||
> (f 1) | ||
#f | ||
> (f 'one) | ||
#t |
Examples: | ||
| ||
> (f 1) | ||
#t | ||
> (f 1.0) | ||
#f | ||
> (f 1/2) | ||
#f | ||
> (f 0.5) | ||
#f |
Examples: | ||
| ||
> (f 1) | ||
#t | ||
> (f 1.0) | ||
#t | ||
> (f 1/2) | ||
#t | ||
> (f 0.5) | ||
#f |
1.3 Currying and (Partial) Application
Examples: | ||||
| ||||
(6 -4 21 1/2) | ||||
| ||||
| ||||
| ||||
| ||||
> (for-each call (list inc inc show reset show)) | ||||
|
| |||
|
((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 |
| ||||
|
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 |
1.4 Eta Expansion
(eta f) |
This is useful for function expressions that may be run, but not called, before f is defined. The eta expression will produce a function without evaluating f.
Examples: | ||
| ||
> f | ||
#<procedure:eta> | ||
| ||
> (f 1) | ||
2 |
(eta* f x ...) |
This macro behaves similarly to eta, but produces a function with statically known arity which may improve efficiency and error reporting.
Examples: | ||
| ||
> f | ||
#<procedure:f> | ||
> (procedure-arity f) | ||
1 | ||
| ||
> (f 1) | ||
2 |
1.5 Parameter Arguments
(lambda/parameter (param-arg ...) body ...) | ||||||||||||||||||||||||||||||
|
Examples: | |||||
| |||||
| |||||
> (hello-world p) | |||||
> (get-output-string p) | |||||
"Hello, World!\n" |