A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.racket-lang.org/reference/procedures.html below:

4.20 Procedures

4.20 Procedures🔗ℹ

Returns #t if v is a procedure, #f otherwise.

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.

Examples:

Returns a procedure that composes the given functions, applying the last

proc

first and the first

proc

last. The

compose

function allows the given functions to consume and produce any number of values, as long as each function produces as many values as the preceding function consumes, while

compose1

restricts the internal value passing to a single value. In both cases, the input arity of the last function and the output arity of the first are unrestricted, and they become the corresponding arity of the resulting composition (including keyword arguments for the input side).

When no proc arguments are given, the result is values. When exactly one is given, it is returned.

Examples:

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.

Returns a procedure that is like

proc

, except that its name as returned by

object-name

(and as printed for debugging) is

name

and its

realm

(potentially used for adjusting error messages) is

realm

.

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.

Reports the

realm

of a procedure, which can depend on the module where the procedure was created, the

current-compile-realm

value when the procedure’s code was compiled, or a realm explicitly assigned through a function like

procedure-rename

.

Added in version 8.4.0.2 of package base.

Returns a procedure that is like

proc

except that, when applied to the wrong number of arguments, the resulting error hides the first argument as if the procedure had been compiled with the

'method-arity-error

syntax property.

Compares the contents of the closures of

proc1

and

proc2

for equality by comparing closure elements pointwise using

eq? 4.20.1 Keywords and Arity🔗ℹ

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.

Examples:

A valid arity a is one of the following:

The result of procedure-arity is always normalized in the sense of normalized-arity?.

Examples:

Returns the same information as

procedure-arity

, but encoded differently. The arity is encoded as an exact integer

mask

where

(bitwise-bit-set? mask n)

returns true if

proc

accepts

n

arguments.

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.

Returns #t if the procedure can accept k by-position arguments, #f otherwise. If kws-ok? is #f, the result is #t only if proc has no required keyword arguments.

Examples:

Returns a procedure that is the same as

proc

(including the same name returned by

object-name

), but that accepts only arguments consistent with

arity

. In particular, when

procedure-arity

is applied to the generated procedure, it returns a value that is

equal?

to the normalized form of

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.

Examples:

> (define my+ (procedure-reduce-arity + 2)) > (my+ 1 2)

3

> (my+ 1 2 3)

+: arity mismatch;

 the expected number of arguments does not match the given

number

  expected: 2

  given: 3

> (define also-my+ (procedure-reduce-arity + 2 'also-my+)) > (also-my+ 1 2 3)

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.

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.

Returns information about the keyword arguments required and accepted by a procedure. The first result is a list of distinct keywords (sorted by

keyword<?

) that are required when applying

proc

. The second result is a list of distinct accepted keywords (sorted by

keyword<?

), or

#f

to mean that any keyword is accepted. When the second result is a list, every element in the first list is also in the second list.

Examples:

Returns the arity of the result of the procedure

proc

or

#f

if the number of results are not known, perhaps due to shortcomings in the implementation of

procedure-result-arity

or because

proc

’s behavior is not sufficiently simple.

Examples:

Added in version 6.4.0.3 of package base.

Returns a procedure that accepts all keyword arguments (without requiring any keyword arguments).

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.

Examples:

> (show 1)

'(() () (1))

> (show #:init 0 1 2 3 #:extra 4)

'((#:extra #:init) (4 0) (1 2 3))

> (show2 1)

'#(1)

> (show2 #:init 0 1 2 3 #:extra 4)

'((#:extra #:init) (4 0) (1 2 3))

Like

procedure-reduce-arity

, but constrains the keyword arguments according to

required-kws

and

allowed-kws

, which must be sorted using

keyword<?

and contain no duplicates. If

allowed-kws

is

#f

, then the resulting procedure still accepts any keyword, otherwise the keywords in

required-kws

must be a subset of those in

allowed-kws

. The original

proc

must require no more keywords than the ones listed in

required-kws

, and it must allow at least the keywords in

allowed-kws

(or it must allow all keywords if

allowed-kws

is

#f

).

Examples:

> (show #:init 0 1 2 3 #:extra 4)

'((#:extra #:init) (4 0) (1 2 3))

> (show 1)

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 #:init 0 1 2 3 #:extra 4 #:more 7)

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.

Added in version 7.0.0.11 of package base.
Changed in version 8.4.0.2: Added the realm argument.

A

structure type property

to identify structure types whose instances can be applied as procedures. In particular, when

procedure?

is applied to the instance, the result will be

#t

, and when an instance is used in the function position of an application expression, a procedure is extracted from the instance and used to complete the procedure call.

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).

Examples:

> (define plus1 (annotated-proc                   (lambda (x) (+ x 1))                   "adds 1 to its argument")) > (procedure? plus1)

#t

> (annotated-proc? plus1)

#t

> (plus1 10)

11

> (annotated-proc-note plus1)

"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).

Examples:

> (define wanda (fish 12 'red)) > (fish? wanda)

#t

> (procedure? wanda)

#t

> (fish-weight wanda)

12

> (for-each wanda '(1 2 3)) > (fish-weight wanda)

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.

Returns

#t

if instances of the structure type represented by

type

are procedures (according to

procedure?

),

#f

otherwise.

If

proc

is an instance of a structure type with property

prop:procedure

, and if the property value indicates a field of the structure, and if the field value is a procedure, then

procedure-extract-target

returns the field value. Otherwise, the result is

#f

.

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.

A

structure type property

that is used for reporting arity-mismatch errors when a structure type with the

prop:procedure

property is applied to the wrong number of arguments. The value of the

prop:arity-string

property must be a procedure that takes a single argument, which is the misapplied structure, and returns a string. The result string is used after the word “expects,” and it is followed in the error message by the number of actual arguments.

Arity-mismatch reporting automatically uses procedure-extract-target when the prop:arity-string property is not associated with a procedure structure type.

Examples:

> (pairs 1 2 3 4)

'((1 . 2) (3 . 4))

> (pairs 5)

arity mismatch;

 the expected number of arguments does not match the given

number

  expected: an even number of arguments

  given: 1

  arguments...:

   5

Extracts a value from

v

if it is an instance of

type

, which must have the property

prop:checked-procedure

. If

v

is such an instance, then the first field of

v

is extracted and applied to

v1

and

v2

; if the result is a true value, the result is the value of the second field of

v

.

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.

Returns proc or its equivalent, but provides a hint to the run-time system that it should spend extra time and memory to specialize the implementation of proc.

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.

4.20.2 Reflecting on Primitives🔗ℹ

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.

Returns #t if v is a primitive procedure, #f otherwise.

Returns #t if v is internally implemented as a primitive closure rather than a simple primitive procedure, #f otherwise.

Returns the arity of the result of the primitive procedure

prim

(as opposed to the procedure’s input arity as returned by

procedure-arity

). For most primitives, this procedure returns

1

, since most primitives return a single value when applied.

4.20.3 Additional Higher-Order Functions🔗ℹ

Returns v.

Returns a procedure that accepts any arguments (including keyword arguments) and returns v.

Examples:

> ((const 'foo))

'foo

> ((const 'foo) 1 2 3)

'foo

> ((const 'foo) 'a 'b #:c 'c)

'foo

Similar to

const

, except it returns

v

s.

Examples:

Added in version 8.7.0.5 of package base.

The

thunk

form creates a nullary function that evaluates the given body. The

thunk*

form is similar, except that the resulting function accepts any arguments (including keyword arguments).

Examples:

(define th1 (thunk (define x 1) (printf "~a\n" x))) > (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

(define th2 (thunk* (define x 1) (printf "~a\n" x))) > (th2)

1

> (th2 'x)

1

> (th2 #:y 'z)

1

Returns a procedure that is just like

proc

, except that it returns the

not

of

proc

’s result.

Examples:

Combines calls to each function with

and

. Equivalent to

(and (f x ...) ...)

Examples:

Combines calls to each function with

or

. Equivalent to

(or (f x ...) ...)

Examples:

The result of

(curry proc)

is a procedure that is a curried version of

proc

. When the resulting procedure is first applied, unless it is given the maximum number of arguments that it can accept according to

(procedure-arity proc)

, the result is a procedure to accept additional arguments.

Examples:

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.

Examples:

A function call (curry proc v ...) is equivalent to ((curry proc) v ...). In other words, curry itself is curried.

Examples:

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.

Examples:

> ((((curry f) #:a 1) #:b 2) #:c 3)

'(1 2 3)

> ((((curry f) #:b 1) #:c 2) #:a 3)

'(3 1 2)

> ((curry f #:a 1 #:c 2) #:b 3)

'(1 3 2)

Changed in version 7.0.0.7 of package base: Added support for keyword arguments.

Like

curry

, except that the arguments are collected in the opposite direction: the first step collects the rightmost group of arguments, and following steps add arguments to the left of these.

Example:

A normalized arity has one of the following forms:

Every normalized arity is a valid procedure arity and satisfies

procedure-arity?

. Any two normalized arity values that are

arity=?

must also be

equal?

.

Examples:

Examples:

Examples:

Returns #true if procedures with arity a accept any number of arguments that procedures with arity b accept.

Examples:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4