OpenMusic

Visual Programming | Computer-Assisted Composition

OpenMusic Reference

Prev| | Next


apply

apply

(LISP function) -- returns the result of the evaluation of function

Syntax

apply function first-arg &restmore-args

Inputs

name data type(s) comments
function symbol, function name or lambda function the function that will be evaulated
first-arg list or number Must be a list if more-args are not used. May be a list if more-args is present and function accepts lists.
more-args list or number repeatable; if used, the last input must be a list.

Output

output data type(s) comments
first any the result of the evaluation

Description

Runs function , passing it first-arg and more-args (if present) as arguments. The last input, whether first-arg or more-args , must be a list. Values from this input are passed individually to function as arguments. In contrast, lists present at any input other than the last are passed in their entirety as arguments. This particularity of the structure of apply exists to make it possible to call extensible functions (functions with &rest inputs).

function may be a symbol, a function name, or function in lambda mode. Note that if you pass a function in lambda mode with repeatable inputs, you must add the correct number to accomodate the number of arguments you are passing it at the other inputs. This is not necessary if the function name is entered directly instead of its icon in lambda mode.

apply is similar to funcall in operation. The difference is that funcall passes all of its inputs as single arguments regardless of whether they are lists ot not.

apply only calls function once. If you need to apply a function multiple times to data in a list or lists, consider using mapcar or mapcan .


Note

LISP functions
The basic LISP functions are too numerous to document comprehensively in this manual. As an aid to the neophyte, the most commonly used have been included in this documentation, but they represent a small fraction of the total functionality of the MCL environment. Numerous on-line resources exist, for instance the online version of Common Lisp the Language, 2nd Edition.

Examples

Summing a list with apply

Here we use apply to add the elements of a list. Since first-arg is the last input present, its elements 1, 2, 3, and 4 are passed individually to the function + as arguments. + could have been the function icon in lambda mode: though note that we would have had to manually add the four inputs for the four arguments. The result, either way:

? OM->10

Creating a list with apply

Here we use apply to create a list, illustrating the particularity of the last input. The arguments are passed sequentially to the function called, in this case, list . The result:

? OM->((7 8) 3 1 2 3 4)

Notice that (7 8) appears in its own parentheses because it was passed as a single argument to list , while 1 2 3 4 appear without, because they were passed individually as arguments, because they occur at the last input.


Prev Home Next
append Up approx-m