In order to understand what the symbol funcall
means I have checked out its documentation. If I understand it right after reading the docs this symbol represents a function which in my eyes can be always easily removed from the elisp expression without changing the outcome:
(funcall '+ 1 2) ; returns 3
(+ 1 2) ; returns 3
Also in the code example provided in the funcall
function documentation the expression works OK after skipping funcall
from it:
(funcall 'cons 'x 'y) ;returns (x . y)
(cons 'x 'y) ;returns (x . y)
What is the difference between the two code versions giving funcall
the right to exist?
Is it just for fun there in order to be able to obfuscate:
(+ 3 2)
expressing it as:
(defun 3+ (x) (+ x 3)) (3+ 2)
(funcall (lambda (x) (+ x 3)) 2)
;; with finally:
(funcall (setq 3+ (lambda (x) (+ x 3))) 2)
?
Or is it there to fix the problem if someone erroneously misused the value slot of a symbol to store there a function?
In other words: is there a code example able to demonstrate a meaningful usage of funcall
?
@Drew : elisp is able to distinguish between the value and the function "slots" of a symbol without usage of funcall
from the context of symbols usage. Run following piece of code in Emacs 29 to see it yourself:
(setq f 2)
(defun f () 3)
(message "val: %s func: %s symb: %s" f (f) 'f)
which gives:
"val: 2 func: 3 symb: f"
@Drew : to my knowledge in elisp ALL symbols and expressions are ALWAYS evaluated. Also 'f
becomes evaluated and it equals to f
only because '
is a syntactical sugar for a special form quote
returning f
as the value of to it passed parameter without evaluating it.
funcall
is a function, which in Lisp means that all of its args are evaluated. Consider that you can pass a variable whose value is a function to afuncall
sexp. That sexp can then invoke whatever function is the variable's value. You can, e.g., usefuncall
in a higher-order function that takes a function as one of its arguments, and that invokes that function. This is how higher-order functions such asmapcar
work.C-h i
).funcall
. Why you would want to store a function in the value cell of a symbol? See my answer for a "real" example.((lambda (...) ...) ...)
, see 10.2.4: “This form is rarely used and is now deprecated.”