Next: , Up: Functions   [Index]


F.2.1 Calling–Invoking Functions

Evaluation

The most common way of invoking a function is by evaluating a list. Evaluating the list ‘(concat "a" "b")’ calls the function concat with arguments "a" and "b".

funcall

Occasionally you need to compute at run time which function to call. To do that, use the function funcall.

Function: funcall function &rest arguments

Call first argument as a function, passing remaining arguments to it.

Return the value that ‘FUNCTION’ returns.

(funcall ’cons ’x ’y) ==> (x . y)

Since funcall is a function, all of its arguments, including ‘FUNCTION’, are evaluated before funcall is called. This means that you can use any expression to obtain the function to be called. It also means that funcall does not see the expressions you write for the arguments, only their values. These values are not evaluated a second time in the act of calling function

The argument ‘FUNCTION’ must be either a Lisp function or a primitive function. Special forms and macros are not allowed.

funcall-interactively

If you need to use funcall to call a command and make it behave as if invoked interactively, use funcall-interactively.

Function: funcall-interactively function &rest arguments

Like funcall but marks the call as interactive. I.e. arrange that within the called function called-interactively-p will return non-‘nil’.

call-interactively

Some functions are user-visible commands, which can be called interactively (usually by a key sequence). It is possible to invoke such a command exactly as though it was called interactively, by using the call-interactively function.

Function: call-interactively function &optional record-flag keys

Call ‘FUNCTION’, providing ‘ARGS’ according to its interactive calling specs. Return the value ‘FUNCTION’ returns.

The function contains a specification of how to do the argument reading. In the case of user-defined functions, this is specified by placing a call to the function interactive at the top level of the function body.

apply

When you also need to determine at run time how many arguments to pass, use apply. apply calls function with ‘ARGUMENTS’, just like funcall but with one difference: the last of ‘ARGUMENTS’ is a list of objects, which are passed to ‘FUNCTION’ as separate arguments, rather than a single list. We say that apply spreads this list so that each individual element becomes an argument.

FUNCTION’ must either be a Lisp function or a primitive function; special forms and macros do not make sense in apply.

Function: apply function &rest arguments

Call ‘FUNCTION’ with remaining ‘args’, using last ‘arg’ as list of ‘args’.

Then return the value ‘FUNCTION’ returns.

With a single argument, call the argument’s first element using the other elements as ‘args’.

(apply '+ 1 2 '(3 4)) ==> 10.

Next: Partial Application, Up: Functions   [Index]