Emacs/Common Lisp: Quote variable's value - emacs

I need the quoted value of a variable.
For example, let's say we have the variables qwe and asd:
(setq qwe '(1 2 3)) ;; qwe is set to (1 2 3)
(setq asd ''(1 2 3)) ;; asd is set to '(1 2 3)
My question is: how do I achieve the same value for asd by using qwe?
I did it this way:
(setq asd `(quote ,qwe))
;; Now asd is '(1 2 3)
But it looks ugly and bad to me. I'll be surprised if there isn't a better way.

You are looking for
(setq asd `',qwe)
which is, in fact, equivalent to
(setq asd `(quote ,qwe))

Related

Behaviour of "#'" in lisp

As far as I understand #' <object> is an abbreviation for (function <object>).
But I noticed different behaviour while using #' in apply function.
Example
(apply '+ '(1 2)) => Works and give 3
(apply (function +) '(1 2 ) ) => 3
However
(apply '# '(1 2)) => Err!!
As described in the reference:
The notation #'name may be used as an abbreviation for (function name).
Note that the syntax used is #', not '#, which is just a way a quoting the symbol #, since 'a is equivalent to (quote a).
So you should use:
(apply #'+ '(1 2))
In general the character # followed by a character has a special meaning for inputting values. For instance #\ is for reading characters (like in #\Space), #( for reading vectors, (like in #(1 2 3)), etc.
The #' notation is implemented by the reader.
If you want to see the effect, you can for example use this:
CL-USER 1 > (read-from-string "#'+")
(FUNCTION +)
3
CL-USER 2 > '#'+
(FUNCTION +)

Apparent 'eval' of quote symbol in CLISP

Some output from the CLISP REPL:
[1]> (list 'list 1 2 3)
(LIST 1 2 3)
[2]> (list 'list '(1 2 3))
(LIST (1 2 3))
[3]> (list 'quote 1 2 3)
(QUOTE 1 2 3)
[4]> (list 'quote '(1 2 3))
'(1 2 3)
The first three, I understand exactly what's going on: The list function is passed a symbol ('list or 'quote) and so the result is a list that begins with the list or quote symbol. It's the fourth that confuses me. Why doesn't it return (QUOTE (1 2 3))?
I realise that if you enter (QUOTE '(1 2 3)) into the REPL, you get '(1 2 3) back, so the expression are equivalent in that sense. But (LIST 1 2 3) is equivalent to (1 2 3), and yet the first expression doesn't return that.
It seems inconsitent that (list 'quote 1 2 3) returns a list with the first item being a quote symbol, but (list 'quote (1 2 3)) returns a quoted list. Especially since expressions like (list 'list ...) seem to always return a list beginning with the symbol - so far, at least, quote is the only 'special case' like this.
It's not the easiest question to articulate, so I'm hoping I've managed to get my confusion across. Can anyone explain why quote gets treated in this seemingly-unique way?
'something is the same as (quote something) for the lisp reader. Even when nested it will be the case. The next expressions I will double quote so that after evaluation one of the quotes are still in there.
When printing the implementations can choose what to output where there are several possible representations, so some implementations would print the evaluation of ''something as
(quote something) while others may use the abbreviation 'something.
'(quote 1 2 3) cannot be abbreviated since a quoted form only has one argument. Thus here both lisp systems would print (quote 1 2 3).
Here is a way to look at your last expression:
(let ((data (list 'quote '(1 2 3))))
(format nil
"whole thing: ~a first element: ~a second-element: ~a"
data
(car data)
(cadr data)))
This will either evaluate to "whole thing: '(1 2 3) first element: QUOTE second-element: (1 2 3)" or "whole thing: (QUOTE (1 2 3)) first element: QUOTE second-element: (1 2 3)".
Since the printer never sees if the input is abbreviated and the data has the same structure in memory the output is never affected by how you input the data. Thus (quote (quote (1 2 3))) will print the same as ''(1 2 3).
You have the same behaviour with cons cells but the standard dictates how the rules are. (cons 1 (cons 2 (cons 3 '()))) would be (1 . (2 . (3 . ()))) but is actually just printed (1 2 3) However if you (cons 1 2) you get (1 . 2) showing that print treats the output differently based on the cdr. However the reader can read any of these and they will all print the same eg. '(1 . (2 . (3 . ()))) ==> (1 2 3) and (+ . (2 . ( 3 . ()))) ; ==> 5
Numbers can have as many visual forms as there are bases below the number in question.
(let ((*print-base* 16))
(print 255)) ; prints FF (255 in hexadecimal)
list does not have any abbreviation or specialness in Lisp. It's not even a primitive function but it's very helpful as it removes the inconvenience of having to cons by hand everytime. It can be defined like this:
(defun my-list (&rest lst)
lst)
(my-list 1 2 3 4) ; ==> (1 2 3 4)
Note that a REPL (the READ-EVAL-PRINT-LOOP) does three things:
reading using the function READ
evaluating using the function EVAL
and printing the result using something like the function PRINT
To understand what is going on you have to look at all three functions.
Let's look at the third form:
(list 'quote 1 2 3)
This is read as a list of five elements:
LIST
(QUOTE QUOTE)
1
2
3
EVAL then evaluates the arguments, and calls the function list with the four results and returns a new result, a list of four elements:
QUOTE
1
2
3
PRINT then takes this list and writes it as: (QUOTE 1 2 3). There is no abbreviated way to print it.
Let's look at the fourth form:
(list 'quote '(1 2 3))
This is read as a list of three elements:
LIST
(QUOTE QUOTE)
(QUOTE (1 2 3))
eval calls list with two arguments:
QUOTE
(1 2 3)
eval then returns a list of length two:
QUOTE
(1 2 3)
print now can print this list in two different ways:
(QUOTE (1 2 3)) or the abbreviated form '(1 2 3). Here a quote character is in front of a single expression.
Your implementation used the first version.

Idiomatic way to destructively add elements to a list?

Is there a nice way to add elements to a list stored in a variable? The ways I'm using aren't very pretty.
Starting list:
(setq sml/hidden-modes (list " hl-p"))
Add items to sml/hidden-modes:
;; First way I append the items to hidden-modes and set it again
(setq sml/hidden-modes (append sml/hidden-modes
(list " AC" " Undo-Tree" " Smrt")))
;; Second way I use add-to-list to add items one at a time instead of all at once
(mapcar (lambda (mode) (add-to-list 'sml/hidden-modes mode))
(list " AC" " Undo-Tree" " Smrt"))
;; Way I see people doing it in random .emacs snippets I find
(add-to-list 'sml/hidden-modes " AC")
(add-to-list 'sml/hidden-modes " Undo-Tree")
(add-to-list 'sml/hidden-modes " Smrt")
If you know in advance that the original list is non-nil, you can try nconc:
ELISP> (setq a '(1 2 3))
(1 2 3)
ELISP> (nconc a '(4 5 6))
(1 2 3 4 5 6)
ELISP> a
(1 2 3 4 5 6)
dash list library has !cons to prepend elements in-place:
(let ((xs '(2 3))) (!cons 1 xs) xs) ; (1 2 3)
Other than that read GNU Emacs Lisp Reference Manual's section Modifying Existing List Structure.

Get a pointer to a list element in Emacs Lisp

For example, I have a list:
(setq foo '(1 2 3 4 5))
Then I need to get a pointer to its 3rd index element (which contains 4 in the example):
(setq p (SOME_FUNCTION foo 3))
The element with p address can be moved to another list so I can't just save its current foo's index.
And I need to be able to say later on:
(push 0 foo)
=> (0 1 2 3 4 5)
(setf p 444)
and list foo must be (0 1 2 3 444 5) afterwards.
Is this possible in Emacs lisp?
In general, you can't store the "address" of an object. However, you can refer to a cons cell (a cons cell is what lists are made of). The cons cell could later be modified using setcar and setcdr.
For example:
(defvar my-cons-cell nil)
(defun my-save-cons-cell (cons-cell)
(setq my-cons-cell cons-cell))
(defun my-set-car-in-saved-cons-cell (value)
(setcar my-cons-cell value))
;; Test
(setq foo '(1 2 3 4 5))
(my-save-cons-cell (cdr (cdr (cdr foo))))
(push 0 foo)
(my-set-car-in-saved-cons-cell 444)
Here, foo has the value (0 1 2 3 444 5).
Note that this is really un-lisp like and breaks the functional programming paradigm...
You can do
(setq p (nth 3 foo))
and it stores in p the value stored at the index you want. You can also do
(setf (nth 3 foo) 444)
to store 444 at that place. But if you try to do something like
(setq pointer (nth 3 foo))
...
(setf pointer 444)
that won't work. In Emacs's trunk I have recently added gv-ref and gv-deref which would work just fine in such a case. They work pretty much like C's & and *:
(setq pointer (gv-ref (nth 3 foo)))
...
(setf (gv-deref pointer) 444)

Resolving symbols in a Common Lisp list

Suppose I have a function
CL-USER> (defun trimmer (seq) "This trims seq and returns a list"
(cdr
(butlast seq)))
TRIMMER
CL-USER> (trimmer '(1 2 3 VAR1 VAR2))
(2 3 VAR1)
CL-USER>
Notice how, due to QUOTE, VAR1 and VAR2 are not resolved. Suppose I want to resolve the symbols VAR1 and VAR2 to their values - is there a standard function to do this?
Do not use quote to create a list with variables; use list instead:
CL-USER> (trimmer (list 1 2 3 var1 var2))
(2 3 value-of-var1)
(where value-of-var1 is the value of var1).
Quote only prevents evaluation of whatever its argument is. If its argument happens to be a list literal, then that is returned. However, to create lists that are not just literals, use list. You can use backquote syntax, but that is rather obfuscation in such a case.
Backquote is the usual way to interpolate values into a quoted list:
> (setq var1 4 var2 5)
5
> `(1 2 3 ,var1 ,var2)
(1 2 3 4 5)
Edited to add: if you want to process a list so that symbols are replaced with their symbol-value, then you need a function something like this:
(defun interpolate-symbol-values (list)
"Return a copy of LIST with symbols replaced by their symbol-value."
(loop for x in list
collect (if (symbolp x) (symbol-value x) x)))
> (interpolate-variables '(1 2 3 var1 var2))
(1 2 3 4 5)
This seems like a strange thing to want to do, however. Can you say more about what you are trying to achieve? Almost certainly there's a better way to do it than this.