all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* alists with precomputed variables
@ 2009-01-21  5:03 Erik Iverson
  2009-01-21  5:13 ` Drew Adams
       [not found] ` <mailman.5420.1232514814.26697.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 4+ messages in thread
From: Erik Iverson @ 2009-01-21  5:03 UTC (permalink / raw)
  To: help-gnu-emacs

Dear all,

I'm an Elisp beginner, just learning over the last few days. Very naive 
question here, with hopefully a simple answer.

I would like to create an alist where one of the values is something 
that I compute earlier.  Example,

I naively thought that

(setq my-var 20)

(setq my-alist '((p1 . 10) (p2 . my-var)))

would give me

((p1 . 10) (p2 . 20))

, but it instead results in

((p1 . 10) (p2 . my-var))

How can I get what I expected?

If this is a wrong-headed question, please let me know.

Thanks,
Erik




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

* RE: alists with precomputed variables
  2009-01-21  5:03 alists with precomputed variables Erik Iverson
@ 2009-01-21  5:13 ` Drew Adams
       [not found] ` <mailman.5420.1232514814.26697.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 4+ messages in thread
From: Drew Adams @ 2009-01-21  5:13 UTC (permalink / raw)
  To: 'Erik Iverson', help-gnu-emacs

> (setq my-var 20)
> (setq my-alist '((p1 . 10) (p2 . my-var)))
> 
> I naively thought that would give me
> ((p1 . 10) (p2 . 20))
> 
> but it instead results in
> ((p1 . 10) (p2 . my-var))
> 
> How can I get what I expected?

(setq my-alist `((p1 . 10) (p2 ,@my-var)))

or (setq my-alist (list '(p1 . 10) (cons 'p2 my-var)))

or (setq my-alist (list (cons 'p1 10) (cons 'p2 my-var)))

See the Elisp manual, node Backquote for info about ` and ,@.
See also nodes Quoting and Building Lists.





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

* Re: alists with precomputed variables
       [not found] ` <mailman.5420.1232514814.26697.help-gnu-emacs@gnu.org>
@ 2009-01-21  5:49   ` Barry Margolin
  2009-01-21  7:54     ` Drew Adams
  0 siblings, 1 reply; 4+ messages in thread
From: Barry Margolin @ 2009-01-21  5:49 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.5420.1232514814.26697.help-gnu-emacs@gnu.org>,
 "Drew Adams" <drew.adams@oracle.com> wrote:

> > (setq my-var 20)
> > (setq my-alist '((p1 . 10) (p2 . my-var)))
> > 
> > I naively thought that would give me
> > ((p1 . 10) (p2 . 20))
> > 
> > but it instead results in
> > ((p1 . 10) (p2 . my-var))
> > 
> > How can I get what I expected?
> 
> (setq my-alist `((p1 . 10) (p2 ,@my-var)))

I guess that might work, but ,@ is supposed to be followed by an 
expression that returns a list, so that it can be spliced into the 
containing list.  It would be better to write:

(setq my-alist `((p1 . 10) (p2 . ,my-var)))

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


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

* RE: alists with precomputed variables
  2009-01-21  5:49   ` Barry Margolin
@ 2009-01-21  7:54     ` Drew Adams
  0 siblings, 0 replies; 4+ messages in thread
From: Drew Adams @ 2009-01-21  7:54 UTC (permalink / raw)
  To: 'Barry Margolin', help-gnu-emacs

> > (setq my-alist `((p1 . 10) (p2 ,@my-var)))
> 
> I guess that might work, but ,@ is supposed to be followed by an 
> expression that returns a list, so that it can be spliced into the 
> containing list.  It would be better to write:
> 
> (setq my-alist `((p1 . 10) (p2 . ,my-var)))

I don't know about "supposed to be" (not only lists are spliced into list
structure), but yes, `.' followed by `,' might be clearer for an atom. But if I
didn't know the value of `my-var', I would probably use `,@', personally.

Anyway, the important thing is to read up on lists in the doc.
There are several ways to get something like what was requested.

The variants I gave are not all equivalent, in any case. In particular, '(p1 .
10) is typically not the same as (cons 'p1 10), in many contexts.





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

end of thread, other threads:[~2009-01-21  7:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-21  5:03 alists with precomputed variables Erik Iverson
2009-01-21  5:13 ` Drew Adams
     [not found] ` <mailman.5420.1232514814.26697.help-gnu-emacs@gnu.org>
2009-01-21  5:49   ` Barry Margolin
2009-01-21  7:54     ` Drew Adams

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.