Version: 5.1.3
14 Functions
This module provides tools for higher-order programming and creating functions.
14.1 Simple Functions
14.2 Higher Order Predicates
Negates the results of
f; equivalent to
(not (f x ...)).
This function is reprovided from scheme/function.
Examples: |
| reference to undefined identifier: negate | > (f 1) | reference to undefined identifier: f | > (f 'one) | reference to undefined identifier: f |
|
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 |
|
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 |
|
14.3 Currying and (Partial) Application
Passes
x ... to
f. Keyword arguments are allowed. Equivalent
to
(f x ...). Useful for application in higher-order contexts.
The
papply and
papplyr functions partially apply
f to
x ..., which may include keyword arguments. They obey the following
equations:
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:
The call, papply, and papplyr utilities are related
to curryn and currynr in the following manner:
Examples: |
| > (reciprocal 3) | 1/3 | > (reciprocal 4) | 1/4 | | (define from-10 (subtract-from 10)) | |
| > (from-10 5) | 5 | > (from-10 10) | 0 | (define from-0 (subtract-from 0)) | |
| > (from-0 5) | -5 | > (from-0 10) | -10 | | > (halve 3) | 3/2 | > (halve 4) | 2 | | (define minus-10 (subtract 10)) | |
| > (minus-10 5) | -5 | > (minus-10 10) | 0 | | > (minus-0 5) | 5 | > (minus-0 10) | 10 |
|
14.4 Eta Expansion
Produces a function equivalent to f, except that f is
evaluated every time it is called.
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 |
|
Produces a function equivalent to
f, with argument list
x ....
In simple cases, this is equivalent to
(lambda (x ...) (f x ...)).
Optional (positional or keyword) arguments are not allowed.
This macro behaves similarly to eta, but produces a function with
statically known arity which may improve efficiency and error reporting.
14.5 Parameter Arguments
(lambda/parameter (param-arg ...) body ...) |
|
param-arg | | = | | param-arg-spec | | | | | | keyword param-spec | | | | | | param-arg-spec | | = | | id | | | | | | [id default-expr] | | | | | | [id #:param param-expr] |
|
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.