4

(describe-function apply) says:

apply is a built-in function in ‘src/eval.c’.                                                                                                                                                                      

(apply FUNCTION &rest ARGUMENTS)                                                                                                                                                                                   

Call FUNCTION with our remaining args, using our last arg as list of 
args.                                                                                                                                         
Then return the value FUNCTION returns.                                                                                                                                                                            
Thus, (apply '+ 1 2 '(3 4)) returns 10.  

(describe-function funcall) says:

funcall is a built-in function in ‘src/eval.c’.                                                                                                                                                                    

(funcall FUNCTION &rest ARGUMENTS)     

Call first argument as a function, passing remaining arguments to it.
Return the value that function returns.
Thus, (funcall 'cons 'x 'y) returns (x . y).

What is the difference between these two function?

2
  • (apply #'+ 1 2 '(3 4)) is the same as (funcall #'+ 1 2 3 4). In other words, (funcall FN X Y Z) is the same as (apply FN (list X Y Z)).
    – Basil
    Commented Nov 19, 2019 at 20:55
  • 2
    "using our last arg as list of args" is the key phrase in the quoted documentation. It means you can say things like (apply #'FUNC ARGS) where ARGS is an arbitrary list which has likely been supplied or generated by other code.
    – phils
    Commented Nov 19, 2019 at 22:00

1 Answer 1

9

Use apply when you don't know what the individual arguments are, or how many there are. You can use funcall (or apply) when you do know that.

For example, suppose your list of args to apply the function to is the value of variable foo. Then you would use (apply 'some-fun foo).

If you know that you're going to apply the function to args 3, toto, and 4, in that order, then you can use (apply 'some-fun (list 3 'toto 4)) or (funcall 'some-fun 3 'toto 4).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.