all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* let-alist can't deal with the keys which includes spaces.
@ 2021-07-20 11:12 Hongyi Zhao
  2021-07-20 12:49 ` tomas
  2021-07-20 15:35 ` Omar Polo
  0 siblings, 2 replies; 12+ messages in thread
From: Hongyi Zhao @ 2021-07-20 11:12 UTC (permalink / raw)
  To: help-gnu-emacs

I adapted the examples given on
<https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html>
to the following:

;;;
(setq colors '(("rose merry" . red) (lily (belladonna . yellow))))

(let-alist colors
  (if (eq ."rose merry" 'red)
      .lily.belladonna))
;;;

When I try to evaluate the above code snippet, the following error is triggered:

Debugger entered--Lisp error: (invalid-read-syntax ". in wrong context" 8 28)
  read(#<buffer *scratch*>)
  elisp--preceding-sexp()
  elisp--eval-last-sexp(t)
  eval-last-sexp(t)
  eval-print-last-sexp(nil)
  funcall-interactively(eval-print-last-sexp nil)
  call-interactively(eval-print-last-sexp nil nil)
  command-execute(eval-print-last-sexp)

It seems that let-alist can't deal with the keys which includes
spaces. Any hints for this problem will be highly appreciated.

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province



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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-20 11:12 let-alist can't deal with the keys which includes spaces Hongyi Zhao
@ 2021-07-20 12:49 ` tomas
  2021-07-20 13:21   ` Hongyi Zhao
  2021-07-20 15:35 ` Omar Polo
  1 sibling, 1 reply; 12+ messages in thread
From: tomas @ 2021-07-20 12:49 UTC (permalink / raw)
  To: Hongyi Zhao; +Cc: help-gnu-emacs

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

On Tue, Jul 20, 2021 at 07:12:14PM +0800, Hongyi Zhao wrote:
> I adapted the examples given on
> <https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html>
> to the following:
> 
> ;;;
> (setq colors '(("rose merry" . red) (lily (belladonna . yellow))))
> 
> (let-alist colors
>   (if (eq ."rose merry" 'red)
>       .lily.belladonna))
> ;;;
> 
> When I try to evaluate the above code snippet, the following error is triggered:

It's not the space. It's the dot ".". This is wrong Lisp syntax:

  (eq ."rose merry" 'red)

Also the dots in .lily.belladonna look strange. This will be the next
syntax error.

BTW., if you take the dot out, like so

  (eq "rose merry" 'red)

the syntax will be correct, but the expression itself won't make much
sense, because it will always be false. The string "rose merry" can't
ever be `eq' to the symbol 'red.

What are you trying to achieve?

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-20 12:49 ` tomas
@ 2021-07-20 13:21   ` Hongyi Zhao
  2021-07-20 14:56     ` tomas
  0 siblings, 1 reply; 12+ messages in thread
From: Hongyi Zhao @ 2021-07-20 13:21 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs

On Tue, Jul 20, 2021 at 8:49 PM <tomas@tuxteam.de> wrote:
>
> On Tue, Jul 20, 2021 at 07:12:14PM +0800, Hongyi Zhao wrote:
> > I adapted the examples given on
> > <https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html>
> > to the following:
> >
> > ;;;
> > (setq colors '(("rose merry" . red) (lily (belladonna . yellow))))
> >
> > (let-alist colors
> >   (if (eq ."rose merry" 'red)
> >       .lily.belladonna))
> > ;;;
> >
> > When I try to evaluate the above code snippet, the following error is triggered:
>
> It's not the space. It's the dot ".". This is wrong Lisp syntax:
>
>   (eq ."rose merry" 'red)
>
> Also the dots in .lily.belladonna look strange. This will be the next
> syntax error.
>
> BTW., if you take the dot out, like so
>
>   (eq "rose merry" 'red)

The dot syntax is described on
<https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html>,
as follows:

Macro: let-alist alist body

Creates a binding for each symbol used as keys the association list
alist, prefixed with dot. This can be useful when accessing several
items in the same association list


> the syntax will be correct, but the expression itself won't make much
> sense, because it will always be false. The string "rose merry" can't
> ever be `eq' to the symbol 'red.
>
> What are you trying to achieve?

I don't want to compare the string "rose merry" and red, instead, the
purpose is to extract the value whose key is "rose merry", and then
compare the result with 'red, just as the example given on
<https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html>
does, i.e., I want to mimic the following one:

(setq colors '((rose . red) (lily (belladonna . yellow) (brindisi . pink))))
(let-alist colors
  (if (eq .rose 'red)
      .lily.belladonna))
=> yellow


But in my case, I changed the key 'rose to "rose merry" which
triggered the error discussed here.

Regards
  - HY



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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-20 13:21   ` Hongyi Zhao
@ 2021-07-20 14:56     ` tomas
  0 siblings, 0 replies; 12+ messages in thread
From: tomas @ 2021-07-20 14:56 UTC (permalink / raw)
  To: Hongyi Zhao; +Cc: help-gnu-emacs

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

On Tue, Jul 20, 2021 at 09:21:32PM +0800, Hongyi Zhao wrote:
> On Tue, Jul 20, 2021 at 8:49 PM <tomas@tuxteam.de> wrote:
> >
> > On Tue, Jul 20, 2021 at 07:12:14PM +0800, Hongyi Zhao wrote:
> > > I adapted the examples given on
> > > <https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html>
> > > to the following:
> > >
> > > ;;;
> > > (setq colors '(("rose merry" . red) (lily (belladonna . yellow))))
> > >
> > > (let-alist colors
> > >   (if (eq ."rose merry" 'red)
> > >       .lily.belladonna))
> > > ;;;
> > >
> > > When I try to evaluate the above code snippet, the following error is triggered:
> >
> > It's not the space. It's the dot ".". This is wrong Lisp syntax:
> >
> >   (eq ."rose merry" 'red)
> >
> > Also the dots in .lily.belladonna look strange. This will be the next
> > syntax error.
> >
> > BTW., if you take the dot out, like so
> >
> >   (eq "rose merry" 'red)
> 
> The dot syntax is described on
> <https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html>,
> as follows:
> 
> Macro: let-alist alist body
>
> Creates a binding for each symbol used as keys the association list
> alist
[...]

Oh -- thanks. Didn't know about `let-alist'. But if I read it correctly,
the macro wants to have symbols as keys, not other types (like strings).
Note the snippet of the doc quote above.

This works for me:

  (setq colors '((rose . "red") (lily . "white") (sunflower . "yellow")))

  (let-alist colors
    (format "a lily is %S\n" .lily))

  => a lily is "white"

It seems `let-alist' gets horribly confused if it tries to build a
variable binding from a string.

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-20 11:12 let-alist can't deal with the keys which includes spaces Hongyi Zhao
  2021-07-20 12:49 ` tomas
@ 2021-07-20 15:35 ` Omar Polo
  2021-07-20 16:20   ` tomas
  1 sibling, 1 reply; 12+ messages in thread
From: Omar Polo @ 2021-07-20 15:35 UTC (permalink / raw)
  To: Hongyi Zhao; +Cc: help-gnu-emacs


Hongyi Zhao <hongyi.zhao@gmail.com> writes:

> I adapted the examples given on
> <https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html>
> to the following:
>
> ;;;
> (setq colors '(("rose merry" . red) (lily (belladonna . yellow))))
>
> (let-alist colors
>   (if (eq ."rose merry" 'red)
>       .lily.belladonna))
> ;;;

It's not an error, it's totally expected.  When you type

	.lily.belladonna

emacs lisp (and I guess every lisp out there) sees the symbol
'.lily.belladonna.

Now, when you type

	."rose merry"

it just doesn't make sense (from the emacs lisp parser POV).  there's a
lonely dot and a string.  A lonely dot is not a valid symbol.  In
addition, remember that spaces aren't strictly necessary.  Consider for
example:

	(let ((.a 5))
	  .a"hello")
	;; => "hello"

	;; equivalent to
	(let ((.a 5))
	  .a
	  "hello")

Strings and symbols are different data types and such have different
properties and different syntax.

Moreover, I don't know how wise is to use strings as alist keys (symbols
are interned, strings doesn't necessary).  I suggest using something
like

	(setq colors '((rose-merry . red) (lily (belladonna . yellow))))

and following the kebab-case style.

HTH

> When I try to evaluate the above code snippet, the following error is triggered:
>
> Debugger entered--Lisp error: (invalid-read-syntax ". in wrong context" 8 28)
>   read(#<buffer *scratch*>)
>   elisp--preceding-sexp()
>   elisp--eval-last-sexp(t)
>   eval-last-sexp(t)
>   eval-print-last-sexp(nil)
>   funcall-interactively(eval-print-last-sexp nil)
>   call-interactively(eval-print-last-sexp nil nil)
>   command-execute(eval-print-last-sexp)
>
> It seems that let-alist can't deal with the keys which includes
> spaces. Any hints for this problem will be highly appreciated.
>
> Regards




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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-20 15:35 ` Omar Polo
@ 2021-07-20 16:20   ` tomas
  2021-07-20 16:52     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: tomas @ 2021-07-20 16:20 UTC (permalink / raw)
  To: Omar Polo; +Cc: help-gnu-emacs, Hongyi Zhao

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

On Tue, Jul 20, 2021 at 05:35:22PM +0200, Omar Polo wrote:
> 
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
> 
> > I adapted the examples given on
> > <https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html>
> > to the following:
> >
> > ;;;
> > (setq colors '(("rose merry" . red) (lily (belladonna . yellow))))
> >
> > (let-alist colors
> >   (if (eq ."rose merry" 'red)
> >       .lily.belladonna))
> > ;;;

[...]

> Strings and symbols are different data types and such have different
> properties and different syntax.

yes, and `let-alist' expects symbols as keys.

> Moreover, I don't know how wise is to use strings as alist keys (symbols
> are interned, strings doesn't necessary).  I suggest using something
> like

In the general case, you can use strings (and other Lisp objects) as keys
in an alist. You have to use the right functions (i.e. `assoc' for the
strings, which by default uses `equal' as equality predicate).

But not for `let-alist'.

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-20 16:20   ` tomas
@ 2021-07-20 16:52     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-20 17:08       ` tomas
  0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-20 16:52 UTC (permalink / raw)
  To: help-gnu-emacs

tomas wrote:

> yes, and `let-alist' expects symbols as keys.

Uhm, what is the purpose with `let-alist' and "dotted symbols"
to begin with?

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




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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-20 16:52     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-20 17:08       ` tomas
  2021-07-20 17:50         ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: tomas @ 2021-07-20 17:08 UTC (permalink / raw)
  To: help-gnu-emacs

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

On Tue, Jul 20, 2021 at 06:52:41PM +0200, Emanuel Berg via Users list for the GNU Emacs text editor wrote:
> tomas wrote:
> 
> > yes, and `let-alist' expects symbols as keys.
> 
> Uhm, what is the purpose with `let-alist' and "dotted symbols"
> to begin with?

`let-alist' is just a handy way to destructure an alist.

Assume we have

  (setq colours '((poppy . red) (cornflower . blue) (sunflower . yellow)))

Instead of doing

  (let ((rose (alist-get 'rose colours))
        (cornflower (alist-get 'cornflower colours))
        (sunflower (alist-get 'sunflower colours)))
    ;; do something with rose, cornflower...
    )

you just do

  (let-alist colours
    ;; do something with .rose, .cornflower...
  )

which is kind of less repetitive. The dots are part of the symbol
name, they aren't special Lisp syntax. Probably they are there to
help avoiding collisions with your variable names. If you don't
name your variables `.foo', all will work out nicely.

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-20 17:08       ` tomas
@ 2021-07-20 17:50         ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-20 18:17           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-20 17:50 UTC (permalink / raw)
  To: help-gnu-emacs

tomas wrote:

>> Uhm, what is the purpose with `let-alist' and "dotted
>> symbols" to begin with?
>
> `let-alist' is just a handy way to destructure an alist.
>
> Assume we have
>
>   (setq colours '((poppy . red) (cornflower . blue) (sunflower . yellow)))
>
> Instead of doing
>
>   (let ((rose (alist-get 'rose colours))
>         (cornflower (alist-get 'cornflower colours))
>         (sunflower (alist-get 'sunflower colours)))
>     ;; do something with rose, cornflower...
>     )
>
> you just do
>
>   (let-alist colours
>     ;; do something with .rose, .cornflower...
>   )

The docstring is confusing IMO, your example so-so but better
because of the initial "`let-alist' is just a handy way to
destructure an alist" line, but why not just put it like this?

(setq numbers '((one   . 1)
                (two   . 2)
                (three . 3)
                (four  . 4) ))

(let-alist numbers
  (list .one .two .three .four) ) ; (1 2 3 4)

> The dots are part of the symbol name, they aren't special
> Lisp syntax.

Perhaps, but in practice they are part of the `let-alist'
special syntax.

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




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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-20 17:50         ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-20 18:17           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-21  7:29             ` tomas
  0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-20 18:17 UTC (permalink / raw)
  To: help-gnu-emacs

> The docstring is confusing IMO, your example so-so but
> better because of the initial "`let-alist' is just a handy
> way to destructure an alist" line, but why not just put it
> like this?
>
> (setq numbers '((one   . 1)
>                 (two   . 2)
>                 (three . 3)
>                 (four  . 4) ))
>
> (let-alist numbers
>   (list .one .two .three .four) ) ; (1 2 3 4)

Or even better, because that's how it is supposed to be used,
right, instead of `let' or as a variety thereof?

  (let-alist '((one   . 1)
               (two   . 2)
               (three . 3)
               (four  . 4) )
    (list .one .two .three .four) ) ; (1 2 3 4)

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




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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-20 18:17           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-21  7:29             ` tomas
  2021-07-21  8:46               ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: tomas @ 2021-07-21  7:29 UTC (permalink / raw)
  To: help-gnu-emacs

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

On Tue, Jul 20, 2021 at 08:17:32PM +0200, Emanuel Berg via Users list for the GNU Emacs text editor wrote:
> > The docstring is confusing IMO, your example so-so but
> > better because of the initial "`let-alist' is just a handy
> > way to destructure an alist" line, but why not just put it
> > like this?
> >
> > (setq numbers '((one   . 1)
> >                 (two   . 2)
> >                 (three . 3)
> >                 (four  . 4) ))
> >
> > (let-alist numbers
> >   (list .one .two .three .four) ) ; (1 2 3 4)
> 
> Or even better, because that's how it is supposed to be used,
> right, instead of `let' or as a variety thereof?
> 
>   (let-alist '((one   . 1)
>                (two   . 2)
>                (three . 3)
>                (four  . 4) )
>     (list .one .two .three .four) ) ; (1 2 3 4)

If the alist is constant anyway, I'd simply use `let'. I'd leave
`let-alist' for cases where the alist is somehow dynamically generated.

But perhaps that's just me :)

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: let-alist can't deal with the keys which includes spaces.
  2021-07-21  7:29             ` tomas
@ 2021-07-21  8:46               ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-21  8:46 UTC (permalink / raw)
  To: help-gnu-emacs

tomas wrote:

> If the alist is constant anyway, I'd simply use `let'.
> I'd leave `let-alist' for cases where the alist is somehow
> dynamically generated.

I was thinking about the base functionality and "dotted
symbol" syntax in particular, but that is the next level,
I agree. DIY tomas :)

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




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

end of thread, other threads:[~2021-07-21  8:46 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-07-20 11:12 let-alist can't deal with the keys which includes spaces Hongyi Zhao
2021-07-20 12:49 ` tomas
2021-07-20 13:21   ` Hongyi Zhao
2021-07-20 14:56     ` tomas
2021-07-20 15:35 ` Omar Polo
2021-07-20 16:20   ` tomas
2021-07-20 16:52     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-20 17:08       ` tomas
2021-07-20 17:50         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-20 18:17           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-21  7:29             ` tomas
2021-07-21  8:46               ` Emanuel Berg via Users list for the GNU Emacs text editor

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.