procedure
(procedure? v)βboolean?
v:any/c
+The apply Function in The Racket Guide introduces apply .
Applies proc using the content of (list* v... lst) as the (by-position) arguments. The #:<kw>kw-arg sequence is also supplied as keyword arguments to proc, where #:<kw> stands for any keyword.
The given proc must accept as many arguments as the number of vs plus length of lst, it must accept the supplied keyword arguments, and it must not require any other keyword arguments; otherwise, the exn:fail:contract exception is raised. The given proc is called in tail position with respect to the apply call.
6
6
0
'((1) (2))
procedure
( compose proc...)βprocedure?
proc:procedure?procedure
( compose1 proc...)βprocedure?
proc:procedure?
When no proc arguments are given, the result is values . When exactly one is given, it is returned.
-3.1622776601683795
0.0+3.1622776601683795i
'(#<path:/> #<path:a> #f)
Note that in many cases, compose1 is preferred. For example, using compose with two library functions may lead to problems when one function is extended to return two values, and the preceding one has an optional input with different semantics. In addition, compose1 may create faster compositions.
procedure
( procedure-rename procname[realm])βprocedure?
proc:procedure?name:symbol?
The given name and realm are used for printing and adjusting an error message if the resulting procedure is applied to the wrong number of arguments. In addition, if proc is an accessor or mutator produced by struct , make-struct-field-accessor , or make-struct-field-mutator , the resulting procedure also uses name when its (first) argument has the wrong type. More typically, however, name is not used for reporting errors, since the procedure name is typically hard-wired into an internal check.
Changed in version 8.4.0.2 of package base: Added the realm argument.
procedure
( procedure-realm proc)βsymbol?
proc:procedure?
Added in version 8.4.0.2 of package base.
procedure
( procedure->method proc)βprocedure?
proc:procedure?
procedure
proc1:procedure?proc2:procedure?
procedure
kw-lstkw-val-lstv...lstproc:procedure?kw-val-lst:list?v:any/clst:list?kw-arg:any/c
+The apply Function in The Racket Guide introduces keyword-apply .
Like apply , but kw-lst and kw-val-lst supply by-keyword arguments in addition to the by-position arguments of the vs and lst, and in addition to the directly supplied keyword arguments in the #:<kw>kw-arg sequence, where #:<kw> stands for any keyword.
The given kw-lst must be sorted using keyword<? . No keyword can appear twice in kw-lst or both in kw-lst and as a #:<kw>, otherwise, the exn:fail:contract exception is raised. The given kw-val-lst must have the same length as kw-lst, otherwise, the exn:fail:contract exception is raised. The given proc must accept all of the keywords in kw-lst plus the #:<kw>s, it must not require any other keywords, and it must accept as many by-position arguments as supplied via the vs and lst; otherwise, the exn:fail:contract exception is raised.
'(1 2 10)
'(1 2 3)
'(1 2 7)
procedure
(procedure-arity proc)βnormalized-arity?
proc:procedure?
procedure
(procedure-arity? v)βboolean?
v:any/c
An exact non-negative integer, which means that the procedure accepts a arguments, only.
A arity-at-least instance, which means that the procedure accepts (arity-at-least-value a) or more arguments.
A list containing integers and arity-at-least instances, which means that the procedure accepts any number of arguments that can match one of the elements of a.
The result of procedure-arity is always normalized in the sense of normalized-arity? .
2
(arity-at-least 0)
#t
0
1
'(1 2)
procedure
(procedure-arity-mask proc)βexact-integer?
proc:procedure?
The mask encoding of an arity is often easier to test and manipulate, and procedure-arity-mask is sometimes faster than procedure-arity while always being at least as fast.
Added in version 7.0.0.11 of package base.
procedure
(procedure-arity-includes? prock[kws-ok?])βboolean?
proc:procedure?
#t
#f
#f
#t
procedure
arity[ nameproc:procedure?arity:procedure-arity?
If the arity specification allows arguments that are not in (procedure-arity proc), the exn:fail:contract exception is raised. If proc accepts keyword argument, either the keyword arguments must be all optional (and they are not accepted in by the arity-reduced procedure) or arity must be the empty list (which makes a procedure that cannot be called); otherwise, the exn:fail:contract exception is raised.
If name is not #f, then object-name of the result procedure produces name, and procedure-realm of the result produced produces realm. Otherwise, object-name and procedure-realm of the result procedure produce the same result as for proc.
> (my+12)3
> (my+123)+: arity mismatch;
the expected number of arguments does not match the given
number
expected: 2
given: 3
> (also-my+123)also-my+: arity mismatch;
the expected number of arguments does not match the given
number
expected: 2
given: 3
Changed in version 7.0.0.11 of package base: Added the optional name
argument.
Changed in version 8.4.0.2: Added the realm argument.
procedure
mask[ nameproc:procedure?mask:exact-integer?
The mask encoding of an arity is often easier to test and manipulate, and procedure-reduce-arity-mask is sometimes faster than procedure-reduce-arity while always being at least as fast.
Added in version 7.0.0.11 of package base.
Changed in version 8.4.0.2: Added the realm argument.
proc:procedure?
'()
'()
'(#:mode #:tag)
'(#:mode #:tag)
'(#:tag)
'(#:mode #:tag)
procedure
(procedure-result-arity proc)β(or/c #fprocedure-arity? )
proc:procedure?
1
#f
(cond#f
Added in version 6.4.0.3 of package base.
procedure
( make-keyword-procedure proc[plain-proc])βprocedure?
plain-proc : procedure?
When the procedure returned by make-keyword-procedure is called with keyword arguments, then proc is called; the first argument is a list of distinct keywords sorted by keyword<? , the second argument is a parallel list containing a value for each keyword, and the remaining arguments are the by-position arguments.
When the procedure returned by make-keyword-procedure is called without keyword arguments, then plain-proc is calledβpossibly more efficiently than dispatching through proc. Normally, plain-proc should have the same behavior as calling proc with empty lists as the first two arguments, but that correspondence is in no way enforced.
The result of procedure-arity and object-name on the new procedure is the same as for plain-proc, if plain-proc is provided. Otherwise, the result of object-name is the same as for proc, but the result of procedure-arity is derived from that of proc by reducing its arity by 2 (i.e., without the two prefix arguments that handle keyword arguments). See also procedure-reduce-keyword-arity and procedure-rename .
> (show1)'(() () (1))
> (show#:init0123#:extra4)'((#:extra #:init) (4 0) (1 2 3))
> (show21)'#(1)
> (show2#:init0123#:extra4)'((#:extra #:init) (4 0) (1 2 3))
procedure
arityrequired-kwsallowed-kws[ nameproc:procedure?arity:procedure-arity?
orig-show3'(#:init)'(#:extra#:init)))> (show#:init0123#:extra4)'((#:extra #:init) (4 0) (1 2 3))
> (show1)arity mismatch;
the expected number of arguments does not match the given
number
expected: 3 plus an argument with keyword #:init plus an
optional argument with keyword #:extra
given: 1
arguments...:
1
> (show#:init0123#:extra4#:more7)application: procedure does not expect an argument with
given keyword
procedure: #<procedure>
given keyword: #:more
arguments...:
1
2
3
#:extra 4
#:init 0
#:more 7
Changed in version 8.4.0.2 of package base: Added the realm argument.
procedure
maskrequired-kwsallowed-kws[ nameproc:procedure?mask:exact-integer?
Added in version 7.0.0.11 of package base.
Changed in version 8.4.0.2: Added the realm argument.
struct
If the prop:procedure property value is an exact non-negative integer, it designates a field within the structure that should contain a procedure. The integer must be between 0 (inclusive) and the number of non-automatic fields in the structure type (exclusive, not counting supertype fields). The designated field must also be specified as immutable, so that after an instance of the structure is created, its procedure cannot be changed. (Otherwise, the arity and name of the instance could change, and such mutations are generally not allowed for procedures.) When the instance is used as the procedure in an application expression, the value of the designated field in the instance is used to complete the procedure call. (This procedure can be another structure that acts as a procedure; the immutability of procedure fields disallows cycles in the procedure graph, so that the procedure call will eventually continue with a non-structure procedure.) That procedure receives all of the arguments from the application expression. The procedureβs name (see object-name ), arity (see procedure-arity ), and keyword protocol (see procedure-keywords ) are also used for the name, arity, and keyword protocol of the structure. If the value in the designated field is not a procedure, then the instance behaves like (case-lambda ) (i.e., a procedure which does not accept any number of arguments). See also procedure-extract-target .
Providing an integer proc-spec argument to make-struct-type is the same as both supplying the value with the prop:procedure property and designating the field as immutable (so that a property binding or immutable designation is redundant and disallowed).
#t
> (annotated-proc?plus1)#t
> (plus110)11
> (annotated-proc-noteplus1)"adds 1 to its argument"
When the prop:procedure value is a procedure, it should accept at least one non-keyword argument. When an instance of the structure is used in an application expression, the property-value procedure is called with the instance as the first argument. The remaining arguments to the property-value procedure are the arguments from the application expression (including keyword arguments). Thus, if the application expression provides five non-keyword arguments, the property-value procedure is called with six non-keyword arguments. The name of the instance (see object-name ) and its keyword protocol (see procedure-keywords ) are unaffected by the property-value procedure, but the instanceβs arity is determined by subtracting one from every possible non-keyword argument count of the property-value procedure. If the property-value procedure cannot accept at least one argument, then the instance behaves like (case-lambda ).
Providing a procedure proc-spec argument to make-struct-type is the same as supplying the value with the prop:procedure property (so that a specific property binding is disallowed).
#:mutable#:property> (fish?wanda)#t
#t
> (fish-weightwanda)12
> (fish-weightwanda)18
If the value supplied for the prop:procedure property is not an exact non-negative integer or a procedure, the exn:fail:contract exception is raised.
procedure
(procedure-struct-type? type)βboolean?
type:struct-type?
procedure
(procedure-extract-target proc)β(or/c #fprocedure? )
proc:procedure?
When a prop:procedure property value is a procedure, the procedure is not returned by procedure-extract-target . Such a procedure is different from one accessed through a structure field, because it consumes an extra argument, which is always the structure that was applied as a procedure. Keeping the procedure private ensures that is it always called with a suitable first argument.
Arity-mismatch reporting automatically uses procedure-extract-target when the prop:arity-string property is not associated with a procedure structure type.
#:propertyprop:arity-string"an even number of arguments"))> (pairs1234)'((1 . 2) (3 . 4))
> (pairs5)arity mismatch;
the expected number of arguments does not match the given
number
expected: an even number of arguments
given: 1
arguments...:
5
procedure
vprocv1type:struct-type?v:any/cv1:any/cv2:any/c
If v is not an instance of type, or if the first field of v applied to v1 and v2 produces #f, then proc is applied to v, v1, and v2, and its result is returned by checked-procedure-check-and-extract .
procedure
(procedure-specialize proc)βprocedure?
proc:procedure?
The hint is currently used when proc is the value of a lambda or case-lambda form that references variables bound outside of the lambda or case-lambda , and when proc has not been previously applied.
Added in version 6.3.0.10 of package base.
A primitive procedure is a built-in procedure that may be implemented in a lower-level language. Not all procedures of racket/base are primitives, but many are. The distinction between primitives and other procedures may be useful to other low-level code.
procedure
(primitive? v)βboolean?
v:any/c
procedure
(primitive-closure? v)βboolean?
v:any/c
procedure
(primitive-result-arity prim)βprocedure-arity?
prim:primitive?
procedure
( const v)βprocedure?
v:any/c
procedure
( const* v...)βprocedure?
v:any/c
'foo
'foo
'foo
'foo
'foo
'foo
'foo
'foo
'foo
Added in version 8.7.0.5 of package base.
> (th1)1
> (th1'x)th1: arity mismatch;
the expected number of arguments does not match the given
number
expected: 0
given: 1
> (th1#:y'z)application: procedure does not accept keyword arguments
procedure: th1
arguments...:
#:y 'z
> (th2)1
> (th2'x)1
> (th2#:y'z)1
procedure
( negate proc)βprocedure?
proc:procedure?
x:any/c
> (f1)#t
> (f1.0)#f
> (f1/2)#f
> (f0.5)#f
1
2
x:any/c
> (f1)#t
> (f1.0)#t
> (f1/2)#t
> (f0.5)#f
1
2
procedure
( curry proc)βprocedure?
proc:procedure?proc:procedure?v:any/c
#<procedure:curried:list>
#<procedure:curried:cons>
'(1 . 2)
After the first application of the result of (curry proc), each further application accumulates arguments until an acceptable number of arguments according to (procedure-arity proc) have been accumulated, at which point the original proc is called.
'(1 2 3)
'(1 3)
6
> (foo123)'(1 2 3)
> (((((foo)1)2))3)'(1 2 3)
A function call (curry procv... ) is equivalent to ((curry proc)v... ). In other words, curry itself is curried.
'(11 12 13)
'(11 12 13)
'(22 24 26)
The curry function also supports functions with keyword arguments: keyword arguments will be accumulated in the same way as positional arguments until all required keyword arguments according to (procedure-keywords proc) have been supplied.
'(1 2 3)
'(3 1 2)
'(1 3 2)
Changed in version 7.0.0.7 of package base: Added support for keyword arguments.
procedure
( curryr proc)βprocedure?
proc:procedure?proc:procedure?v:any/c
procedure
( normalized-arity? arity)βboolean?
arity:any/c
the empty list;
an exact non-negative integer;
an arity-at-least instance;
a list of two or more strictly increasing, exact non-negative integers; or
a list of one or more strictly increasing, exact non-negative integers followed by a single arity-at-least instance whose value is greater than the preceding integer by at least 2.
#t
#f
#t
#f
#f
procedure
( normalize-arity arity)
arity:procedure-arity?
1
1
(arity-at-least 2)
(arity-at-least 2)
(arity-at-least 1)
(arity-at-least 1)
(arity-at-least 2)
(arity-at-least 2)
(list 0 2 (arity-at-least 4))
#t
#t
#t
#f
#f
#t
#t
#f
#f
#t
#t
#f
#f
procedure
( arity-includes? ab)βboolean?
#t
#t
#t
#f
#t
#t
#t
#t
#f
#t
#t
#f
#t