Emacs: What does the "#" before function symbol imply? [duplicate] - emacs

This question already has answers here:
When should Emacs #'function syntax be used?
(3 answers)
Closed 6 years ago.
For example: (add-hook 'after-init-hook #'global-flycheck-mode)
Why # needs to be prepended to 'global-flycheck-mode?

#' is just a short-hand for using function. From the elisp manual:
-- Special Form: function function-object
This special form returns FUNCTION-OBJECT without evaluating it.
In this, it is similar to ‘quote’ (see Quoting). But unlike
‘quote’, it also serves as a note to the Emacs evaluator and
byte-compiler that FUNCTION-OBJECT is intended to be used as a
function. Assuming FUNCTION-OBJECT is a valid lambda expression,
this has two effects:
• When the code is byte-compiled, FUNCTION-OBJECT is compiled
into a byte-code function object (see Byte Compilation).
• When lexical binding is enabled, FUNCTION-OBJECT is converted
into a closure. See Closures.
You can see the difference when byte-compiling/loading this
(setq f1 '(lambda (x) (* x x)))
(setq f2 #'(lambda (x) (* x x)))
Only the the correctly quoted form is byte-compiled:
(byte-code-function-p f1)
nil
(byte-code-function-p f2)
t

Related

Is there a difference between `#'(lambda... ` and `(lambda...`? [duplicate]

This question already has answers here:
Why #' is used before lambda in Common Lisp?
(2 answers)
Writing lambda expressions in common lisp
(1 answer)
Closed 2 years ago.
In Practical Common Lisp there is a example of REMOVE-IF-NOT with a lambda:
CL-USER> (remove-if-not #'(lambda (x) (evenp x)) '(1 2 3 4 5))
(2 4)
Is this any different from:
CL-USER> (remove-if-not (lambda (x) (evenp x)) '(1 2 3 4 5))
(2 4)
Is a (lambda..) value coincident to the quoted-function form #'(..)? On the REPL it seems so, but as I'm new to Lisp I may overlook something (and I surely got the verbiage wrong, so please correct me on that too).
These two things are, as you suspect, the same:
#' is a read macro, and #'x is read as (function x). So #'(lambda (...) ...) is read as (function (lambda (...) ...)), where function is the special operator in CL which says that its argument denotes a function;
lambda is defined as a macro: the expansion of (lambda (...) ...) is (function (lambda (...) ...): the identical form to the previous one.
This means that #'(lambda (...) ...), (lambda (...) ...) and (function (lambda (...) ...)) all are the same thing in CL: they all denote the function specified by (lambda (...) ...) in the current lexical environment.
There are two reasons why people might still use the #'(lambda (...) ...) version:
consistency – you need, for instance #'foo if foo is a function, so it may be seen as more consistent to use it for the lambda case too;
it was not always the case in CL that lambda had the macro definition it now does, so code that was written between the original CL specification and the ANSI standard, or which was written by people who remember those times, may use the #' form.

What does the `#` mean in elisp? [duplicate]

This question already has answers here:
What is the role of the # character in Emacs Lisp?
(2 answers)
Closed 7 years ago.
Quite likely this is a dumb question, but I haven't come across the # symbol in the bits of elisp I have read, and was wondering what it means (preceded by a , as well) in this code? I have had some difficulty providing the proper search phrase I think.
In case of link rot:
(defmacro zenburn-with-color-variables (&rest body)
"`let' bind all colors defined in `zenburn-colors-alist' around BODY.
Also bind `class' to ((class color) (min-colors 89))."
(declare (indent 0))
`(let ((class '((class color) (min-colors 89)))
,#(mapcar (lambda (cons)
(list (intern (car cons)) (cdr cons)))
zenburn-colors-alist))
,#body))
This is an elisp macro definition, it defines a template for code to be substituted by other code at compile time. A decent intro is chapter 7 of Paul Graham's On Lisp
http://www.paulgraham.com/onlisptext.html
Ask Emacs, by checking the index of the elisp manual:
C-hig (elisp) RET
I # RET
Follow result: * ,# (with backquote) [Index]: Backquote. (line 29)
You can also "splice" an evaluated value into the resulting list,
using the special marker ‘,#’. The elements of the spliced list become
elements at the same level as the other elements of the resulting list.
The equivalent code without using ‘`’ is often unreadable. Here are
some examples:
[...]

Are symbols and names different?

Are symbols and names different? On Lisp by Paul Graham, which focuses on common lisp, has some discussions that seem to imply so, e.g.
Since lambda-expressions are also names of functions, they can also appear first in function calls:
((lambda (x) (* x 2) 3)
6
This makes it sound like symbols are names but names aren't symbols. But I don't understand what sort of Lisp "object" symbols are / could be.
This is also deriving from my question here on the sharp-quote (#') operator v. symbol-function. I'm suspecting the only reason these are different is because not all names are symbols but I don't have enough background to understand those answers yet (hence this question).
I'm also asking for clarification in elisp v. common lisp. I'm assuming this pertains to lexical form, which wasn't introduced in elisp until version 24 (24.1 I think).
Lambda Expressions are not names of functions. It's just that ((lambda (...) ...) ...) is allowed in Common Lisp, since it is defined in the standard as legal syntax.
The only allowed function names in Common Lisp are symbols and lists like (setf symbol).
For example one can write
(defun (setf foo) (...) ...)
Here (setf foo) is the function name.
Other function names don't exist in Common Lisp, only symbols and (setf symbol) names.
Common Lisp Hyperspec Glossary: Function Name.
function name n. 1. (in an environment) A symbol or a list (setf
symbol) that is the name of a function in that environment. 2. A
symbol or a list (setf symbol).
Note: the Common Lisp version of 1984 (as published in CLtL1) did only have symbols as function names. Thus the idea of a function name was not defined. The function to retrieve a function from a symbol was called SYMBOL-FUNCTION. In 1989 the ANSI CL standardization group decided to add setf lists as function names. It also introduced the function FDEFINITION, which is like SYMBOL-FUNCTION but also accepts other function names, besides symbols. See here: Issue FUNCTION-NAME.
I think Rainer's answer gets this right, but since the question appeared in a comment on my answer to another question, I'll include (an update to) my response from that comment.
A symbol is an actual object. You can inspect it, you can create them with make-symbol, etc. Importantly, symbols are one of the primary components of source code in Common Lisp. Function names, especially in the context that this question arose in (arguments to the function special operator) are either symbols or lists of the form (setf symbol) according to the glossary entry:
function name n. 1. (in an environment) A symbol or a list (setf symbol) that is the name of a function in that environment. 2. A
symbol or a list (setf symbol).
Function is a special operator that doesn't evaluate its arguments, so passing a symbol or setf list means something like:
(function car)
(function (setf car))
and not:
(function 'car)
(function '(setf car))
Now, lexical variables, e.g., x in (let ((x 42)) x), while represented by symbols in the source code, don't actually have any connection with the symbol at runtime. The compiled version of (let ((x 42)) x) doesn't need to know anything about the symbol x. Intuitively, this makes sense, because we'd expect the code (let ((y 42)) y) to compile to the same thing. However, when a variable is special, there is a connection with the symbol. The difference is clearest with:
(let ((x 42))
(symbol-value x))
;=> NIL
(let ((x 42))
(declare (special x)) ; or (defparameter x ...) or (defvar x ...) earlier
(symbol-value x))
;=> 42
We'd expect the same thing to be true of lexically scoped functions, e.g., the following code causes an error because there's no connection between the symbol x at runtime and the local function:
(flet ((x () 42))
(symbol-function 'x)) ; ERROR, no function value for symbol x
But even so, we can still do:
(flet ((x () 42))
(function x))
This is because function is special operator and can access the environment where is occurs. That means that (because it's special, and the implementation makes it work) it can know that x is defined as function here. It may be interesting to note, now, that since flet and labels are defined to take a function name, you can do:
(flet (((setf kar) (value kons)
...))
...)

emacs: get lexically bound variable value by name [duplicate]

This question already has answers here:
Lexical eval in emacs24
(3 answers)
Closed 9 years ago.
The following does not work with void variable error. What should eval be replaced with to get this to work?
;; -*- lexical-binding: t -*-
(defun foo2 ()
(let ((b 'lkj))
(lambda ()
(eval 'b t))))
(funcall (foo2))
symbol-value doesn't work either (as documented).
Looking up a variable's value by name is fundamentally incompatible with proper lexical scoping, because proper lexical scoping admits alpha-renaming, i.e. (consistently) renaming a variable should not affect the result.
Of course, if you really must know, you can hack things around in some cases, doing things like:
(funcall `(closure ,(nth 1 <someclosure>) () <exp>))
which might evaluate <exp> in the same scope as the one from where <someclosure> comes. It won't work if <someclosure> was byte-compiled (or if I made a mistake).

Higher-order functions in Elisp

I created a function that returns a function in Elisp:
(defun singleton-set (elem)
(defun f (n) (= n elem))
f)
I try to run this in IELM, and it fails:
ELISP> (singleton-set 5)
*** Eval error *** Symbol's value as variable is void: f
ELISP> ((singleton-set 5) 5)
*** Eval error *** Invalid function: (singleton-set 5)
Due to What is the difference between Lisp-1 and Lisp-2? i changed the code to
(defun singleton-set (elem)
(defun f (n) (= n elem))
#'f)
And invocation to (funcall (singleton-set 5) 5), but now the error is
*** Eval error *** Symbol's value as variable is void: elem
I understand from elisp: capturing variable from inner function that this is due to dynamic binding of Emacs Lisp.
How to make functions returning functions possible in Emacs Lisp? What is the reason this mechanism is different from other languages like Python, Scala or Clojure?
Related questions:
elisp functions as parameters and as return value
Elisp interactive function name
How to Create a Temporary Function in Emacs Lisp
In elisp, how do I put a function in a variable?
From the NEWS for Emacs 24:
Lisp changes in Emacs 24.1
Code can now use lexical scoping by default instead of dynamic scoping.
The lexical-binding variable enables lexical scoping for local
variables. It is typically set via a file-local variable in the first
line of the file, in which case it applies to all the code in that
file.
So, in Emacs 24:
(setq lexical-binding t)
(defun singleton-set (elem) (lambda (n) (= n elem)))
(mapcar (singleton-set 1) '(0 1 2 3))
===> (nil t nil nil)
How to make functions returning functions possible in Emacs Lisp?
Using fake closures, and lexical-let.
What is the reason this mechanism is different from other languages like Python, Scala or Clojure?
Richard Stallman answered this question in a paper he wrote a while ago.
(defun singleton-set (elem)
`(lambda (n) (= n ,elem))
See: elisp functions as parameters and as return value