unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* append, push, and add-to-list.
@ 2021-07-04  3:18 Hongyi Zhao
  2021-07-04  4:35 ` Jean Louis
  2021-07-04 16:19 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 22+ messages in thread
From: Hongyi Zhao @ 2021-07-04  3:18 UTC (permalink / raw)
  To: help-gnu-emacs

I've noticed there are some similar functions, i.e., append, push, and
add-to-list, for revising/updating the content of a list. But I'm
still not so clear on the specific use scenarios for them. Consider
the following command [1]:

(push '(company-capf company-ispell) company-backends)

What's the equivalent representations using append and add-to-list?
Which methods are preferable?

[1] <https://github.com/company-mode/company-mode/issues/1124#issuecomment-864461337>

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-04  3:18 append, push, and add-to-list Hongyi Zhao
@ 2021-07-04  4:35 ` Jean Louis
  2021-07-04  5:11   ` Hongyi Zhao
  2021-07-04 16:19 ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 22+ messages in thread
From: Jean Louis @ 2021-07-04  4:35 UTC (permalink / raw)
  To: Hongyi Zhao; +Cc: help-gnu-emacs

* Hongyi Zhao <hongyi.zhao@gmail.com> [2021-07-04 06:19]:
> I've noticed there are some similar functions, i.e., append, push, and
> add-to-list, for revising/updating the content of a list. But I'm
> still not so clear on the specific use scenarios for them. Consider
> the following command [1]:
> 
> (push '(company-capf company-ispell) company-backends)
> 
> What's the equivalent representations using append and add-to-list?
> Which methods are preferable?

It is recommended to review documentation for each function by using
{C-h f} -

- function add-to-list is traditionally rather used in configurations,
  how I see it, like (add-to-list 'load-path "/my/directory") or
  similar, instead, in programming code one shall rather use `push' or
  `cl-pushnew' as `add-to-list' adds only new unique elements;

Because you use it more for configurations, then add-to-list would be:

(add-to-list 'company-backends '(company-capf company-ispell))

and with append

(append company-backends '(company-capf company-ispell))



-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-04  4:35 ` Jean Louis
@ 2021-07-04  5:11   ` Hongyi Zhao
  2021-07-04 11:28     ` Jean Louis
  2021-07-04 16:20     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 22+ messages in thread
From: Hongyi Zhao @ 2021-07-04  5:11 UTC (permalink / raw)
  To: Hongyi Zhao, help-gnu-emacs

On Sun, Jul 4, 2021 at 12:36 PM Jean Louis <bugs@gnu.support> wrote:
>
> * Hongyi Zhao <hongyi.zhao@gmail.com> [2021-07-04 06:19]:
> > I've noticed there are some similar functions, i.e., append, push, and
> > add-to-list, for revising/updating the content of a list. But I'm
> > still not so clear on the specific use scenarios for them. Consider
> > the following command [1]:
> >
> > (push '(company-capf company-ispell) company-backends)
> >
> > What's the equivalent representations using append and add-to-list?
> > Which methods are preferable?
>
> It is recommended to review documentation for each function by using
> {C-h f} -
>
> - function add-to-list is traditionally rather used in configurations,
>   how I see it, like (add-to-list 'load-path "/my/directory") or
>   similar, instead, in programming code one shall rather use `push' or
>   `cl-pushnew' as `add-to-list' adds only new unique elements;

Thank you. I listed the usages below for all above-mentioned functions
by {C-h f} function-name:

(add-to-list LIST-VAR ELEMENT &optional APPEND COMPARE-FN)
(push NEWELT PLACE)
(cl-pushnew X PLACE [KEYWORD VALUE]...)
(append &rest SEQUENCES)

>
> Because you use it more for configurations, then add-to-list would be:
>
> (add-to-list 'company-backends '(company-capf company-ispell))

Why can't this be written as follows, just like the other two?

(add-to-list company-backends '(company-capf company-ispell))

>
> and with append
>
> (append company-backends '(company-capf company-ispell))

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-04  5:11   ` Hongyi Zhao
@ 2021-07-04 11:28     ` Jean Louis
  2021-07-04 13:50       ` Hongyi Zhao
  2021-07-04 16:20     ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 22+ messages in thread
From: Jean Louis @ 2021-07-04 11:28 UTC (permalink / raw)
  To: Hongyi Zhao; +Cc: help-gnu-emacs

* Hongyi Zhao <hongyi.zhao@gmail.com> [2021-07-04 08:12]:
> Thank you. I listed the usages below for all above-mentioned functions
> by {C-h f} function-name:
> 
> (add-to-list LIST-VAR ELEMENT &optional APPEND COMPARE-FN)
> (push NEWELT PLACE)
> (cl-pushnew X PLACE [KEYWORD VALUE]...)
> (append &rest SEQUENCES)

Good, I can also see some possible inconsistencies with X and NEWELT,
as it would be better to call X also NEWELT. I can also see that
documentation for `cl-pushnew' is not adequate enough. You are
probably expected to learn definition from Common Lisp to understand
the KEYWORDs:
http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/mac_pushnew.html#pushnew

Function `append' will work with various sequences not just lists.

Please see manual: (info "(elisp) Sequences Arrays Vectors")

> > Because you use it more for configurations, then add-to-list would be:
> >
> > (add-to-list 'company-backends '(company-capf company-ispell))
> 
> Why can't this be written as follows, just like the other two?
> 
> (add-to-list company-backends '(company-capf company-ispell))
> 
> >
> > and with append
> >
> > (append company-backends '(company-capf company-ispell))

Functions `add-to-list' and `append' do different things, though
similar. 


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-04 11:28     ` Jean Louis
@ 2021-07-04 13:50       ` Hongyi Zhao
  2021-07-04 15:58         ` Jean Louis
  2021-07-04 16:01         ` [External] : " Drew Adams
  0 siblings, 2 replies; 22+ messages in thread
From: Hongyi Zhao @ 2021-07-04 13:50 UTC (permalink / raw)
  To: Hongyi Zhao, help-gnu-emacs

On Sun, Jul 4, 2021 at 7:32 PM Jean Louis <bugs@gnu.support> wrote:
>
> * Hongyi Zhao <hongyi.zhao@gmail.com> [2021-07-04 08:12]:
> > Thank you. I listed the usages below for all above-mentioned functions
> > by {C-h f} function-name:
> >
> > (add-to-list LIST-VAR ELEMENT &optional APPEND COMPARE-FN)
> > (push NEWELT PLACE)
> > (cl-pushnew X PLACE [KEYWORD VALUE]...)
> > (append &rest SEQUENCES)
>
> Good, I can also see some possible inconsistencies with X and NEWELT,
> as it would be better to call X also NEWELT. I can also see that
> documentation for `cl-pushnew' is not adequate enough. You are
> probably expected to learn definition from Common Lisp to understand
> the KEYWORDs:
> http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/mac_pushnew.html#pushnew
>
> Function `append' will work with various sequences not just lists.
>
> Please see manual: (info "(elisp) Sequences Arrays Vectors")
>
> > > Because you use it more for configurations, then add-to-list would be:
> > >
> > > (add-to-list 'company-backends '(company-capf company-ispell))
> >
> > Why can't this be written as follows, just like the other two?
> >
> > (add-to-list company-backends '(company-capf company-ispell))
> >
> > >
> > > and with append
> > >
> > > (append company-backends '(company-capf company-ispell))
>
> Functions `add-to-list' and `append' do different things, though
> similar.
>

I'm curious why `add-to-list' must take 'company-backends, i.e.,
(quote company-backends) as one of its argument, while the other
functions can directly use the unquoted form.

HY
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-04 13:50       ` Hongyi Zhao
@ 2021-07-04 15:58         ` Jean Louis
  2021-07-05  2:04           ` Hongyi Zhao
  2021-07-04 16:01         ` [External] : " Drew Adams
  1 sibling, 1 reply; 22+ messages in thread
From: Jean Louis @ 2021-07-04 15:58 UTC (permalink / raw)
  To: Hongyi Zhao; +Cc: help-gnu-emacs

* Hongyi Zhao <hongyi.zhao@gmail.com> [2021-07-04 16:52]:
> I'm curious why `add-to-list' must take 'company-backends, i.e.,
> (quote company-backends) as one of its argument, while the other
> functions can directly use the unquoted form.

It must not. But add-to-list is by tradition older and more used
function. And it will not require cl library such as `cl-pushnew'.

I am verifying:

(type-of company-backends) ⇒ cons

(company-bbdb company-eclim company-semantic company-clang
	      company-xcode company-cmake company-capf company-files
	      (company-dabbrev-code company-gtags company-etags
	      company-keywords) company-oddmuse company-dabbrev)

This is the list, so you may use any method you wish to put elements
in the list.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 22+ messages in thread

* RE: [External] : Re: append, push, and add-to-list.
  2021-07-04 13:50       ` Hongyi Zhao
  2021-07-04 15:58         ` Jean Louis
@ 2021-07-04 16:01         ` Drew Adams
  1 sibling, 0 replies; 22+ messages in thread
From: Drew Adams @ 2021-07-04 16:01 UTC (permalink / raw)
  To: Hongyi Zhao, help-gnu-emacs

> I'm curious why `add-to-list' must take 'company-backends, i.e.,
> (quote company-backends) as one of its argument, while the other
> functions can directly use the unquoted form.

It's not that it "must take" a quoted symbol as arg.
It's that takes a symbol as arg.  That symbol can be
provided any way you like, i.e., by evaluating any
sexp that results in a symbol.

It's essentially equivalent to using both a `pushnew'
and a `set' or `setq' (with a variable as PLACE).  It
sets the variable value of its symbol arg to the new
list value.

As others have said, you will do yourself a favor by
reading (even studying) the Elisp manual.

Likewise, by looking at the Elisp source code provided
with Emacs.  The source code is freely available;
consider taking advantage of that fact.

`grep' the source code and use `flush-lines' to remove
instances where the first arg to `add-to-list' is a
quoted symbol, to get a feel for its use evaluating a
sexp (other than a quoted symbol) that yields a symbol.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-04  3:18 append, push, and add-to-list Hongyi Zhao
  2021-07-04  4:35 ` Jean Louis
@ 2021-07-04 16:19 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-05  2:16   ` Hongyi Zhao
  1 sibling, 1 reply; 22+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-04 16:19 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> I've noticed there are some similar functions, i.e., append,
> push, and add-to-list, for revising/updating the content of
> a list. But I'm still not so clear on the specific use
> scenarios for them. Consider the following command [1]

`nconc', `cl-pushnew', (setq lst `(,@lst el)) ...

> [...] Which methods are preferable?

Good question, perhaps in the manual somewhere they listed
and explained all in one place, if not, the help (`C-h f')
should be your friend.

But my intuition tells me, just because there are many options
a, b, ..., and z, that doesn't mean necessarily that one is
optimal for that situation.

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-04  5:11   ` Hongyi Zhao
  2021-07-04 11:28     ` Jean Louis
@ 2021-07-04 16:20     ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 22+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-04 16:20 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> Thank you. I listed the usages below for all above-mentioned
> functions by {C-h f} function-name:
>
> (add-to-list LIST-VAR ELEMENT &optional APPEND COMPARE-FN)
> (push NEWELT PLACE)
> (cl-pushnew X PLACE [KEYWORD VALUE]...)
> (append &rest SEQUENCES)

But the syntax is probably the easy part...

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-04 15:58         ` Jean Louis
@ 2021-07-05  2:04           ` Hongyi Zhao
  0 siblings, 0 replies; 22+ messages in thread
From: Hongyi Zhao @ 2021-07-05  2:04 UTC (permalink / raw)
  To: Hongyi Zhao, help-gnu-emacs

On Mon, Jul 5, 2021 at 12:01 AM Jean Louis <bugs@gnu.support> wrote:
>
> * Hongyi Zhao <hongyi.zhao@gmail.com> [2021-07-04 16:52]:
> > I'm curious why `add-to-list' must take 'company-backends, i.e.,
> > (quote company-backends) as one of its argument, while the other
> > functions can directly use the unquoted form.
>
> It must not. But add-to-list is by tradition older and more used
> function. And it will not require cl library such as `cl-pushnew'.
>
> I am verifying:
>
> (type-of company-backends) ⇒ cons
>
> (company-bbdb company-eclim company-semantic company-clang
>               company-xcode company-cmake company-capf company-files
>               (company-dabbrev-code company-gtags company-etags
>               company-keywords) company-oddmuse company-dabbrev)


See my results obtained by putting `(type-of company-backends)' in
scratch buffer, and with the point on any part of `company-backends',
then hit `M-.':

(defcustom company-backends `(company-bbdb
                              ,@(unless (version<= "26" emacs-version)
                                  (list 'company-nxml))
                              ,@(unless (version<= "26" emacs-version)
                                  (list 'company-css))
                              company-semantic
                              company-cmake
                              company-capf
                              company-clang
                              company-files
                              (company-dabbrev-code company-gtags company-etags
                               company-keywords)
                              company-oddmuse company-dabbrev)


>
> This is the list, so you may use any method you wish to put elements
> in the list.
>
> --
> Jean
>
> Take action in Free Software Foundation campaigns:
> https://www.fsf.org/campaigns
>
> In support of Richard M. Stallman
> https://stallmansupport.org/



-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-04 16:19 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-05  2:16   ` Hongyi Zhao
  2021-07-05  3:48     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-05  5:06     ` Jean Louis
  0 siblings, 2 replies; 22+ messages in thread
From: Hongyi Zhao @ 2021-07-05  2:16 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Mon, Jul 5, 2021 at 12:19 AM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > I've noticed there are some similar functions, i.e., append,
> > push, and add-to-list, for revising/updating the content of
> > a list. But I'm still not so clear on the specific use
> > scenarios for them. Consider the following command [1]
>
> `nconc', `cl-pushnew', (setq lst `(,@lst el)) ...

Does the following append `el' to `lst'?

(setq lst `(,@lst el))

>
> > [...] Which methods are preferable?
>
> Good question, perhaps in the manual somewhere they listed
> and explained all in one place, if not, the help (`C-h f')
> should be your friend.
>
> But my intuition tells me, just because there are many options
> a, b, ..., and z, that doesn't mean necessarily that one is
> optimal for that situation.

This is one of the reasons that baffles me the most - there are so
many functions in Emacs.

HY
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-05  2:16   ` Hongyi Zhao
@ 2021-07-05  3:48     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-05  4:31       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-05  5:06     ` Jean Louis
  1 sibling, 1 reply; 22+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-05  3:48 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> On Mon, Jul 5, 2021 at 12:19 AM Emanuel Berg via Users list for the
> GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
>>
>> Hongyi Zhao wrote:
>>
>> > I've noticed there are some similar functions, i.e., append,
>> > push, and add-to-list, for revising/updating the content of
>> > a list. But I'm still not so clear on the specific use
>> > scenarios for them. Consider the following command [1]
>>
>> `nconc', `cl-pushnew', (setq lst `(,@lst el)) ...
>
> Does the following append `el' to `lst'?
>
> (setq lst `(,@lst el))

Yes, literally, but maybe it makes more sense if that is
evaluated as well?

(defvar lst)
(setq lst '(a b c))

(defvar el)
(setq el 4)

(setq lst `(,@lst ,el))

Now "lst" will be (a b c 4)

> This is one of the reasons that baffles me the most - there
> are so many functions in Emacs.

The more the merrier...

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-05  3:48     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-05  4:31       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-05  5:07         ` Hongyi Zhao
  0 siblings, 1 reply; 22+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-05  4:31 UTC (permalink / raw)
  To: help-gnu-emacs

>> Does the following append `el' to `lst'?
>>
>> (setq lst `(,@lst el))
>
> Yes, literally, but maybe it makes more sense if that is
> evaluated as well?
>
> (defvar lst)
> (setq lst '(a b c))
>
> (defvar el)
> (setq el 4)
>
> (setq lst `(,@lst ,el))
>
> Now "lst" will be (a b c 4)

(setq lst '(a b))
(setq lst `(,@lst logic) ; (a b logic)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-05  2:16   ` Hongyi Zhao
  2021-07-05  3:48     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-05  5:06     ` Jean Louis
  2021-07-05  6:22       ` Hongyi Zhao
  1 sibling, 1 reply; 22+ messages in thread
From: Jean Louis @ 2021-07-05  5:06 UTC (permalink / raw)
  To: Hongyi Zhao; +Cc: help-gnu-emacs, Emanuel Berg

* Hongyi Zhao <hongyi.zhao@gmail.com> [2021-07-05 05:17]:
> On Mon, Jul 5, 2021 at 12:19 AM Emanuel Berg via Users list for the
> GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> >
> > Hongyi Zhao wrote:
> >
> > > I've noticed there are some similar functions, i.e., append,
> > > push, and add-to-list, for revising/updating the content of
> > > a list. But I'm still not so clear on the specific use
> > > scenarios for them. Consider the following command [1]
> >
> > `nconc', `cl-pushnew', (setq lst `(,@lst el)) ...
> 
> Does the following append `el' to `lst'?
> 

(defvar lst '()) ⇒ lst

(setq lst `(,@lst el)) ⇒ (el)

lst  ⇒ (el)

I see it does when variable `list' is defined in advance.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-05  4:31       ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-05  5:07         ` Hongyi Zhao
  2021-07-05  5:50           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 22+ messages in thread
From: Hongyi Zhao @ 2021-07-05  5:07 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Mon, Jul 5, 2021 at 12:32 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> >> Does the following append `el' to `lst'?
> >>
> >> (setq lst `(,@lst el))
> >
> > Yes, literally, but maybe it makes more sense if that is
> > evaluated as well?
> >
> > (defvar lst)
> > (setq lst '(a b c))
> >
> > (defvar el)
> > (setq el 4)
> >
> > (setq lst `(,@lst ,el))
> >
> > Now "lst" will be (a b c 4)
>
> (setq lst '(a b))
> (setq lst `(,@lst logic) ; (a b logic)
>

You have a typo here, should be the following:

((setq lst `(,@lst logic))

Wonderful examples, but how to eval multiple line lisp codes in
scratch buffer? I only know with twice hitting of `C-j' for this job,
as shown below:

(setq lst '(a b)) <--- C-j
(a b)

((setq lst `(,@lst logic)) <-- C-j
(a b logic)

HY
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-05  5:07         ` Hongyi Zhao
@ 2021-07-05  5:50           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-05  6:19             ` Hongyi Zhao
  0 siblings, 1 reply; 22+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-05  5:50 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

>> (setq lst '(a b))
>> (setq lst `(,@lst logic) ; (a b logic)
>
> You have a typo here, should be the following:
>
> ((setq lst `(,@lst logic))

Heh, yes but now there is another typo :)

It should look like this:

  (setq lst '(a b))
  (setq lst `(,@lst logic)) ; (a b logic)

> Wonderful examples, but how to eval multiple line lisp codes
> in scratch buffer? I only know with twice hitting of `C-j'
> for this job, as shown below:

You can use `eval-buffer' or enclose it in a `progn' perhaps.
Or, for this particular example, `let'/`let*'...

I have M-9 do `eval-last-sexp', more ergonomic IMO than
whatever is the default binding...

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-05  5:50           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-05  6:19             ` Hongyi Zhao
  0 siblings, 0 replies; 22+ messages in thread
From: Hongyi Zhao @ 2021-07-05  6:19 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Mon, Jul 5, 2021 at 1:51 PM Emanuel Berg via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> >> (setq lst '(a b))
> >> (setq lst `(,@lst logic) ; (a b logic)
> >
> > You have a typo here, should be the following:
> >
> > ((setq lst `(,@lst logic))
>
> Heh, yes but now there is another typo :)
>
> It should look like this:
>
>   (setq lst '(a b))
>   (setq lst `(,@lst logic)) ; (a b logic)
>

Yes. Thanks again.

> > Wonderful examples, but how to eval multiple line lisp codes
> > in scratch buffer? I only know with twice hitting of `C-j'
> > for this job, as shown below:
>
> You can use `eval-buffer'

I tried with `eval-buffer' but can't see the expected output.

> or enclose it in a `progn' perhaps.
> Or, for this particular example, `let'/`let*'...
>
> I have M-9 do `eval-last-sexp', more ergonomic IMO than
> whatever is the default binding...
>

HY
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-05  5:06     ` Jean Louis
@ 2021-07-05  6:22       ` Hongyi Zhao
  2021-07-05  6:32         ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 22+ messages in thread
From: Hongyi Zhao @ 2021-07-05  6:22 UTC (permalink / raw)
  To: Hongyi Zhao, Emanuel Berg, help-gnu-emacs

On Mon, Jul 5, 2021 at 1:11 PM Jean Louis <bugs@gnu.support> wrote:
>
> * Hongyi Zhao <hongyi.zhao@gmail.com> [2021-07-05 05:17]:
> > On Mon, Jul 5, 2021 at 12:19 AM Emanuel Berg via Users list for the
> > GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> > >
> > > Hongyi Zhao wrote:
> > >
> > > > I've noticed there are some similar functions, i.e., append,
> > > > push, and add-to-list, for revising/updating the content of
> > > > a list. But I'm still not so clear on the specific use
> > > > scenarios for them. Consider the following command [1]
> > >
> > > `nconc', `cl-pushnew', (setq lst `(,@lst el)) ...
> >
> > Does the following append `el' to `lst'?
> >
>
> (defvar lst '()) ⇒ lst
>

(equal '() nil)
t

So, I would rather write it like this:

(defvar lst nil)

HY

> (setq lst `(,@lst el)) ⇒ (el)
>
> lst  ⇒ (el)
>
> I see it does when variable `list' is defined in advance.
>
> --
> Jean
>
> Take action in Free Software Foundation campaigns:
> https://www.fsf.org/campaigns
>
> In support of Richard M. Stallman
> https://stallmansupport.org/



-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: append, push, and add-to-list.
  2021-07-05  6:22       ` Hongyi Zhao
@ 2021-07-05  6:32         ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 22+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-05  6:32 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> (equal '() nil)
> t
>
> So, I would rather write it like this:
>
> (defvar lst nil)

But with '() you see that it is (an empty) list:

  (defvar lst)
  (setq lst '())

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 22+ messages in thread

* RE: [External] : Re: append, push, and add-to-list.
  2021-07-05 15:19   ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-07-05 16:18     ` Drew Adams
  0 siblings, 0 replies; 22+ messages in thread
From: Drew Adams @ 2021-07-05 16:18 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

[-- Attachment #1: Type: text/plain, Size: 321 bytes --]

> You should quote () when you feel like it, and you should not quote it
> when you don't feel like it.

Yes.

In general, one might also want to consider that one
feels like writing for other human readers as well. ;-)

But in this case, () vs '(), it's unlikely that either
choice will confuse other readers.

[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 13100 bytes --]

^ permalink raw reply	[flat|nested] 22+ messages in thread

* RE: [External] : Re: append, push, and add-to-list.
  2021-07-05 14:53 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-05 15:19   ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-07-05 16:19   ` Drew Adams
  2021-07-05 16:41     ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 22+ messages in thread
From: Drew Adams @ 2021-07-05 16:19 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

[-- Attachment #1: Type: text/plain, Size: 961 bytes --]

> An unquoted list would be, for example, (list 1 2 3) So to
> keep it consistent, without a quote, would it be
>   (defvar lst)
>   (setq lst (list)) ; nil
> ?

If you really want, yes you can write `(list)'.
That evaluates to nil just like nil, '(), and () do.

`list' is a function.  `(list ...)' is a function call.

() is not a function call.  It's a constant.
It's just alternative _syntax_ for nil.

'() is syntax for the special form `(quote ())'.

All of `(list)', (), '(), and nil evaluate to nil.

> '() makes it easier to spot immediately, hey,
> that's an empty list.

To each their own. ;-)  I don't find '() easier
to spot than ().

> But ... ultimately one writes code so the computer can execute
> it, and not so that a human can read it. So maybe one
> shouldn't quote empty lists?
> 
> If so, I have 14 corrections to make...

No, the computer couldn't care less whether
you write (list), nil, (), or '(). ;-)

[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 13396 bytes --]

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [External] : Re: append, push, and add-to-list.
  2021-07-05 16:19   ` Drew Adams
@ 2021-07-05 16:41     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 22+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-05 16:41 UTC (permalink / raw)
  To: help-gnu-emacs

Drew Adams wrote:

> To each their own. ;-) I don't find '() easier to spot than
> ().

It is easier to spot because it has an additional apostrophe
(or apostrophe-quote) compared to (), that makes it more
visibly and makes it stick out compared to all the (( and ))
of `let', `cond', and more, and in general, from all the
parenthesis of Lisp.

>> But ... ultimately one writes code so the computer can
>> execute it, and not so that a human can read it. So maybe
>> one shouldn't quote empty lists?
>> 
>> If so, I have 14 corrections to make...
>
> No, the computer couldn't care less whether
> you write (list), nil, (), or '(). ;-)

... but you yourself said `list' is a function, nil is
a constant (hm... didn't know we had those -

(defun c-constant-symbol (sym len)
  "Create an uneditable symbol for customization buffers.
SYM is the name of the symbol, LEN the length of the field (in
characters) the symbol will be displayed in.  LEN must be big
enough.

This returns a (const ....) structure, suitable for embedding
within a customization type."
  (or (symbolp sym) (error "c-constant-symbol: %s is not a symbol" sym))
  (let* ((name (symbol-name sym))
	 (l (length name))
	 (disp (concat name ":" (make-string (- len l 1) ?\ ))))
    `(const
      :size ,len
      :format ,disp
      :value ,sym))))

- perhaps that's something else since it seems to be just
a list where the first element is const - but here, nil is
unsuited for other reasons - and () is a constant (another,
here more suited syntax, for nil), and '() is another syntax
for (quote ()), which is `quote' a special form?

Maybe the "special form" thing is that it doesn't eval
the argument?

  (setf (quote ...) ...)

Both list and quote are built-in, IOW written in C?

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2021-07-05 16:41 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-07-04  3:18 append, push, and add-to-list Hongyi Zhao
2021-07-04  4:35 ` Jean Louis
2021-07-04  5:11   ` Hongyi Zhao
2021-07-04 11:28     ` Jean Louis
2021-07-04 13:50       ` Hongyi Zhao
2021-07-04 15:58         ` Jean Louis
2021-07-05  2:04           ` Hongyi Zhao
2021-07-04 16:01         ` [External] : " Drew Adams
2021-07-04 16:20     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-04 16:19 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-05  2:16   ` Hongyi Zhao
2021-07-05  3:48     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-05  4:31       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-05  5:07         ` Hongyi Zhao
2021-07-05  5:50           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-05  6:19             ` Hongyi Zhao
2021-07-05  5:06     ` Jean Louis
2021-07-05  6:22       ` Hongyi Zhao
2021-07-05  6:32         ` Emanuel Berg via Users list for the GNU Emacs text editor
  -- strict thread matches above, loose matches on Subject: below --
2021-07-05 14:37 Drew Adams
2021-07-05 14:53 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-05 15:19   ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-07-05 16:18     ` [External] : " Drew Adams
2021-07-05 16:19   ` Drew Adams
2021-07-05 16:41     ` Emanuel Berg via Users list for the GNU Emacs text editor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).