(setq callback (lambda ()
         (message "hai i am a lambda")))
(funcall callback)

(setq const-val "hai, i am a const")
(message const-val)

prints as

hai i am a lambda
hai, i am a const

Both of them are working perfect. that raises question about setq behavior. Does it put the argument value in the symbol's function cell (or) variable cell?

  • 1
    Did you ask Emacs? What does C-h f setq tell you? What does C-h i, choose Elisp manual, i setq tell you? Such a question is trivial to answer - setq sets a variable's value. – Drew Feb 26 '15 at 16:44
  • I'm voting to close this question as off-topic because it shows no attempt to first find the answer by asking Emacs. – Drew Feb 26 '15 at 16:45
  • 4
    @Drew, I dont think that qualify a question for being off topic, and I dont think setq's documentation would have answered OPs question. He seems to be already under the impression that setq should only set the variable value, but is confused about how funcall or quoted symbols work. – Jordon Biondo Feb 26 '15 at 17:24
  • @JordonBiondo: IMO, it should qualify a question as off-topic. A minimum of effort should be required up front, IMHO. At english.stackexchange.com, for instance, questions are routinely closed if they do not "include the research you've done". Similarly, StackOverflow requires you to indicate what you've tried so far, to find the answer. – Drew Feb 26 '15 at 17:45
  • 2
    @JordonBiondo, thanks a ton for your reply. the first time, i saw setq taking a function param, i got surprised which prompted me to raise this qn. and believe me, i am using emacs for 6 months and got a clear understanding of single quote only after seeing the answers. thanks. – Madhavan Feb 26 '15 at 22:38

It places it in the variable cell. The following example shows the distinction:

*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (setq callback (lambda () (message "I am a lambda in a variable cell")))
(lambda nil
  (message "I am a lambda in a variable cell"))

ELISP> (defun callback () (message "I am a function in a function cell"))
ELISP> (funcall callback) ;; Call the function in the variable (expand the symbol)
"I am a lambda in a variable cell"
ELISP> (funcall 'callback) ;; Call the function defined by the symbol
"I am a function in a function cell"

It always uses the variable cell. You can tell because you are passing callbacks variable value into funcall.

Try running (funcall 'callback), you will get an error, because callback has no value as a function, only a variable.

Here you can see the value of your symbols as variables and functions:

(setq callback (lambda () (message "hai i am a lambda")))
(setq const-val "hai, i am a const")

(symbol-value 'callback) ;; => (lambda nil (message "hai i am a lambda"))
(symbol-function 'callback) ;; => nil

(symbol-value 'const-val) ;; => "hai, i am a const"
(symbol-function 'const-val) ;; => nil

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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