When to Use EVAL, APPLY, FUNCALL

The three forms eval, apply, and funcall allow efficient calling of functions, depending on the form of existing data.

Use eval when the existing data is already Lisp code:


(eval (subst 10.0 'r
             '(* 3.1415926 (expt r 2))))

Use apply when the existing data is already a list of arguments:


(setq nums '(3 7 21 14))
(apply #'+ nums)

Use funcall when the existing data is in the form of individual arguments:


(setq x 7)
(setq y 3)
(funcall #'+ x y)

Contents    Page-10    Prev    Next    Page+10    Index