I'm learning elisp, and I just learned that the first element of a list is interpreted as a function symbol. I then learned how to define a function with defun
. Here's the example from An Introduction to Programming in Emacs Lisp
(defun multiply-by-seven (number)
"Multiply NUMBER by seven."
(* 7 number))
https://www.gnu.org/software/emacs/manual/html_node/eintr/defun.html
Now it looks the first element of the argument list [(number)
in the example above] is not regarded as a function symbol.
Given a list in an elisp code, how do I know if its first element is a function or not?
Addendum after seeing answer and comments by whitetrillium
I got the idea that there's distinction between symbol (i.e., variable) and value.
My question is the following: when I read an elisp code, how can I judge if the code fragment (e.g., number, 7, multiply-by-seven, defun in the example above) refers to the symbol itself or the value to be obtained by evaluating the symbol.
Somewhat relevant is the distinction between set
and setq
functions. Please see these examples:
(set 'flowers '(rose violet daisy buttercup))
(setq carnivores '(lion tiger leopard))
https://www.gnu.org/software/emacs/manual/html_node/eintr/Using-set.html https://www.gnu.org/software/emacs/manual/html_node/eintr/Using-setq.html
In set
function, the first argument is not treated as a symbol, but it is evaluated to its value. To avoid the evaluation, it needs to be quoted. In contrast, in setq
function, the first argument is treated as a symbol, and it is not evaluated.
I can guess that it doesn't make sense for (number)
to be evaluated in the particular example of defun
because it's a placeholder. I also know that the first argument to setq
is treated as a symbol because I learned it from the tutorial. However, if I see the following code,
(hoge (foo bar))
how do I know whether (foo bar)
is evaluated by passing argument bar
to function foo
or it is treated as a list of two elements, (foo bar)
? I guess I might need to look into the source code which defines hoge
. Then, how can I write function hoge
in these two ways, and where does the difference appear?
setq
is not a function: it it aspecial form
. It is special precisely because it does not evaluate its first argument. See Special Forms. In contrast, a function call is done by first evaluating the first element to make sure it is af function, evaluating all the other elements of the list and then calling the function with the values of the elements bound to its params. See Evaluation of function forms