When do you use “apply” and when “funcall”?

前端 未结 3 1159
遥遥无期
遥遥无期 2021-02-04 02:33

The Common Lisp HyperSpec says in the funcall entry that

(funcall function arg1 arg2 ...) 
==  (apply function arg1 arg2 ... nil) 
==  (app         


        
3条回答
  •  无人及你
    2021-02-04 03:18

    apply is useful when the argument list is known only at runtime, especially when the arguments are read dynamically as a list. You can still use funcall here but you have to unpack the individual arguments from the list, which is inconvenient. You can also use apply like funcall by passing in the individual arguments. The only thing it requires is that the last argument must be a list:

    > (funcall #'+ 1 2)
    3
    > (apply #'+ 1 2 ())
    3
    

提交回复
热议问题