all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to use a symbol and its value to create alist?
@ 2015-08-11 13:52 Navy Cheng
  2015-08-11 15:21 ` Ian Zimmerman
  0 siblings, 1 reply; 11+ messages in thread
From: Navy Cheng @ 2015-08-11 13:52 UTC (permalink / raw)
  To: help-gnu-emacs

For example:

(setq a 1)
(setq b 2)
(setq c 3)

How can I a alist, like:
((a . 1) (b . 2) (c .3))

The value of a, b and c may change, so don't do this like
(setq tree ((a . 1) (b . 2) (c .3)))

Thank you!




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

* Re: How to use a symbol and its value to create alist?
       [not found] <mailman.8135.1439301205.904.help-gnu-emacs@gnu.org>
@ 2015-08-11 14:39 ` Pascal J. Bourguignon
  2015-08-12  1:57   ` Navy Cheng
  2015-08-12  0:21 ` Barry Margolin
  1 sibling, 1 reply; 11+ messages in thread
From: Pascal J. Bourguignon @ 2015-08-11 14:39 UTC (permalink / raw)
  To: help-gnu-emacs

Navy Cheng <navych@126.com> writes:

> For example:
>
> (setq a 1)
> (setq b 2)
> (setq c 3)
>
> How can I a alist, like:

How can you WHAT a alist?

> ((a . 1) (b . 2) (c .3))
>
> The value of a, b and c may change, so don't do this like
> (setq tree ((a . 1) (b . 2) (c .3)))

This is not a valid form, because (a . 1) is not a function name,
therefore it's not possible to apply it.



If you want to BUILD an a-list, you can use acons:

(let ((a 1)
      (b 2)
      (c 3))
  (let ((tree (acons 'a a (acons 'b b (acons 'c c '())))))
     tree))
--> ((a . 1) (b . 2) (c . 3))


-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


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

* Re: How to use a symbol and its value to create alist?
  2015-08-11 13:52 How to use a symbol and its value to create alist? Navy Cheng
@ 2015-08-11 15:21 ` Ian Zimmerman
  2015-08-12  1:46   ` Navy Cheng
       [not found]   ` <mailman.8160.1439343998.904.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 11+ messages in thread
From: Ian Zimmerman @ 2015-08-11 15:21 UTC (permalink / raw)
  To: help-gnu-emacs

On 2015-08-11 21:52 +0800, Navy Cheng wrote:

> (setq a 1)
> (setq b 2)
> (setq c 3)
> 
> How can I a alist, like:
> ((a . 1) (b . 2) (c .3))
> 
> The value of a, b and c may change, so don't do this like
> (setq tree ((a . 1) (b . 2) (c .3)))

That's a strange question.  Why would you want such a list, how would
it be useful?  To look up the value a a symbol, you just use it, for
example: 

(setq a 1)

...

(message "a=%d" a)

=> a=1

In some special situations where a symbol is not evaluated (such as when
playing with macros) you may need to use symbol-value.  But I'm guessing
you're not at that point.

-- 
Please *no* private copies of mailing list or newsgroup messages.
Rule 420: All persons more than eight miles high to leave the court.




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

* Re: How to use a symbol and its value to create alist?
       [not found] <mailman.8135.1439301205.904.help-gnu-emacs@gnu.org>
  2015-08-11 14:39 ` Pascal J. Bourguignon
@ 2015-08-12  0:21 ` Barry Margolin
  2015-08-12  1:35   ` Navy Cheng
  1 sibling, 1 reply; 11+ messages in thread
From: Barry Margolin @ 2015-08-12  0:21 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.8135.1439301205.904.help-gnu-emacs@gnu.org>,
 Navy Cheng <navych@126.com> wrote:

> For example:
> 
> (setq a 1)
> (setq b 2)
> (setq c 3)
> 
> How can I a alist, like:
> ((a . 1) (b . 2) (c .3))
> 
> The value of a, b and c may change, so don't do this like
> (setq tree ((a . 1) (b . 2) (c .3)))
> 
> Thank you!

I suspect this is what you want:

(setq tree (list (cons 'a a) (cons 'b b) (cons 'c c)))

or using backquote:

(setq tree `((a . ,a) (b . ,b) (c . ,c)))

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


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

* Re: How to use a symbol and its value to create alist?
  2015-08-12  0:21 ` Barry Margolin
@ 2015-08-12  1:35   ` Navy Cheng
  0 siblings, 0 replies; 11+ messages in thread
From: Navy Cheng @ 2015-08-12  1:35 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, Aug 11, 2015 at 07:21:15PM -0500, Barry Margolin wrote:
> In article <mailman.8135.1439301205.904.help-gnu-emacs@gnu.org>,
>  Navy Cheng <navych@126.com> wrote:
> 
> > For example:
> > 
> > (setq a 1)
> > (setq b 2)
> > (setq c 3)
> > 
> > How can I a alist, like:
> > ((a . 1) (b . 2) (c .3))
> > 
> > The value of a, b and c may change, so don't do this like
> > (setq tree ((a . 1) (b . 2) (c .3)))
> > 
> > Thank you!
> 
> I suspect this is what you want:
> 
> (setq tree (list (cons 'a a) (cons 'b b) (cons 'c c)))
> 
> or using backquote:
> 
> (setq tree `((a . ,a) (b . ,b) (c . ,c)))
> 
Thank you for your help, This is what I want.




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

* Re: How to use a symbol and its value to create alist?
  2015-08-11 15:21 ` Ian Zimmerman
@ 2015-08-12  1:46   ` Navy Cheng
       [not found]   ` <mailman.8160.1439343998.904.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 11+ messages in thread
From: Navy Cheng @ 2015-08-12  1:46 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, Aug 11, 2015 at 08:21:53AM -0700, Ian Zimmerman wrote:
> On 2015-08-11 21:52 +0800, Navy Cheng wrote:
> 
> > (setq a 1)
> > (setq b 2)
> > (setq c 3)
> > 
> > How can I a alist, like:
> > ((a . 1) (b . 2) (c .3))
> > 
> > The value of a, b and c may change, so don't do this like
> > (setq tree ((a . 1) (b . 2) (c .3)))
> 
> That's a strange question.  Why would you want such a list, how would
> it be useful?  To look up the value a a symbol, you just use it, for
> example: 

I need to push some global variable to a "stack" and pop them later. If
I don't do like this, the global variables will be changed by program

> (setq a 1)
> 
> ...
> 
> (message "a=%d" a)
> 
> => a=1
> 
> In some special situations where a symbol is not evaluated (such as when
> playing with macros) you may need to use symbol-value.  But I'm guessing
> you're not at that point.
>

My question has been solved. What I want is

    (setq trees `((a . ,a) (b . ,b) (c . ,c)))

Thanks any way.




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

* Re: How to use a symbol and its value to create alist?
  2015-08-11 14:39 ` Pascal J. Bourguignon
@ 2015-08-12  1:57   ` Navy Cheng
  0 siblings, 0 replies; 11+ messages in thread
From: Navy Cheng @ 2015-08-12  1:57 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, Aug 11, 2015 at 04:39:45PM +0200, Pascal J. Bourguignon wrote:
> Navy Cheng <navych@126.com> writes:
> 
> > For example:
> >
> > (setq a 1)
> > (setq b 2)
> > (setq c 3)
> >
> > How can I a alist, like:
> 
> How can you WHAT a alist?

Sorry for that, (setq WHAT "create")

> 
> > ((a . 1) (b . 2) (c .3))
> >
> > The value of a, b and c may change, so don't do this like
> > (setq tree ((a . 1) (b . 2) (c .3)))
> 
> This is not a valid form, because (a . 1) is not a function name,
> therefore it's not possible to apply it.
> 

Sorry again, (setq tree '((a . 1) (b . 2) (c .3)))

> 
> If you want to BUILD an a-list, you can use acons:
> 
> (let ((a 1)
>       (b 2)
>       (c 3))
>   (let ((tree (acons 'a a (acons 'b b (acons 'c c '())))))
>      tree))
> --> ((a . 1) (b . 2) (c . 3))
> 
Thanks for your patient and help. Your answer is OK. I have learned
another way to do this.

(setq tree `((a . ,a) (b . ,b) (c . ,c)))




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

* Re: How to use a symbol and its value to create alist?
       [not found]   ` <mailman.8160.1439343998.904.help-gnu-emacs@gnu.org>
@ 2015-08-12  2:12     ` Pascal J. Bourguignon
  2015-08-12  2:18       ` Pascal J. Bourguignon
  0 siblings, 1 reply; 11+ messages in thread
From: Pascal J. Bourguignon @ 2015-08-12  2:12 UTC (permalink / raw)
  To: help-gnu-emacs

Navy Cheng <navych@126.com> writes:

> On Tue, Aug 11, 2015 at 08:21:53AM -0700, Ian Zimmerman wrote:
>> On 2015-08-11 21:52 +0800, Navy Cheng wrote:
>> 
>> > (setq a 1)
>> > (setq b 2)
>> > (setq c 3)
>> > 
>> > How can I a alist, like:
>> > ((a . 1) (b . 2) (c .3))
>> > 
>> > The value of a, b and c may change, so don't do this like
>> > (setq tree ((a . 1) (b . 2) (c .3)))
>> 
>> That's a strange question.  Why would you want such a list, how would
>> it be useful?  To look up the value a a symbol, you just use it, for
>> example: 
>
> I need to push some global variable to a "stack" and pop them later. If
> I don't do like this, the global variables will be changed by program

    (defvar a 1)
    (defvar b 2)
    (defvar cc 3)

    (defun do-something ()
      (print (list 'before a b cc))
      (setf a 0 b 0 cc 0)
      (print (list 'after a b cc)))

    (progn
      (let ((a a)
            (b b)
            (cc cc))
         (do-something))
      (list 'finally a b cc))
    prints:

    (before 1 2 3)

    (after 0 0 0)
    --> (finally 1 2 3)

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


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

* Re: How to use a symbol and its value to create alist?
  2015-08-12  2:12     ` Pascal J. Bourguignon
@ 2015-08-12  2:18       ` Pascal J. Bourguignon
  2015-08-12  5:06         ` Navy Cheng
       [not found]         ` <mailman.8170.1439355992.904.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 11+ messages in thread
From: Pascal J. Bourguignon @ 2015-08-12  2:18 UTC (permalink / raw)
  To: help-gnu-emacs

"Pascal J. Bourguignon" <pjb@informatimago.com> writes:

> Navy Cheng <navych@126.com> writes:
>
>> On Tue, Aug 11, 2015 at 08:21:53AM -0700, Ian Zimmerman wrote:
>>> On 2015-08-11 21:52 +0800, Navy Cheng wrote:
>>> 
>>> > (setq a 1)
>>> > (setq b 2)
>>> > (setq c 3)
>>> > 
>>> > How can I a alist, like:
>>> > ((a . 1) (b . 2) (c .3))
>>> > 
>>> > The value of a, b and c may change, so don't do this like
>>> > (setq tree ((a . 1) (b . 2) (c .3)))
>>> 
>>> That's a strange question.  Why would you want such a list, how would
>>> it be useful?  To look up the value a a symbol, you just use it, for
>>> example: 
>>
>> I need to push some global variable to a "stack" and pop them later. If
>> I don't do like this, the global variables will be changed by program
>
>     (defvar a 1)
>     (defvar b 2)
>     (defvar cc 3)
>
>     (defun do-something ()
>       (print (list 'before a b cc))
>       (setf a 0 b 0 cc 0)
>       (print (list 'after a b cc)))
>
>     (progn
>       (let ((a a)
>             (b b)
>             (cc cc))
>          (do-something))
>       (list 'finally a b cc))
>     prints:
>
>     (before 1 2 3)
>
>     (after 0 0 0)
>     --> (finally 1 2 3)

If you have a lot of global variables you want to preserve like this, or
in a lot of places, you can write a macro:

    (defmacro with-saved-variables (variables &rest body)
      `(let ,(mapcar (lambda (var) (list var var)) variables) 
         ,@body))

     (progn
       (with-saved-variables (a b cc)
          (do-something))
       (list 'finally a b cc))
    prints:
    (before 1 2 3)

    (after 0 0 0)
    --> (finally 1 2 3)

and if you wonder why cc, it's because I have a constant named c ->
299792458.0 and this is the reason why you should name your global
variables, which are special, with stars: *a* *b* *c*.


-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


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

* Re: How to use a symbol and its value to create alist?
  2015-08-12  2:18       ` Pascal J. Bourguignon
@ 2015-08-12  5:06         ` Navy Cheng
       [not found]         ` <mailman.8170.1439355992.904.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 11+ messages in thread
From: Navy Cheng @ 2015-08-12  5:06 UTC (permalink / raw)
  To: help-gnu-emacs

On Wed, Aug 12, 2015 at 04:18:04AM +0200, Pascal J. Bourguignon wrote:
> "Pascal J. Bourguignon" <pjb@informatimago.com> writes:
> 
> > Navy Cheng <navych@126.com> writes:
> >
> >> On Tue, Aug 11, 2015 at 08:21:53AM -0700, Ian Zimmerman wrote:
> >>> On 2015-08-11 21:52 +0800, Navy Cheng wrote:
> >>> 
> >>> > (setq a 1)
> >>> > (setq b 2)
> >>> > (setq c 3)
> >>> > 
> >>> > How can I a alist, like:
> >>> > ((a . 1) (b . 2) (c .3))
> >>> > 
> >>> > The value of a, b and c may change, so don't do this like
> >>> > (setq tree ((a . 1) (b . 2) (c .3)))
> >>> 
> >>> That's a strange question.  Why would you want such a list, how would
> >>> it be useful?  To look up the value a a symbol, you just use it, for
> >>> example: 
> >>
> >> I need to push some global variable to a "stack" and pop them later. If
> >> I don't do like this, the global variables will be changed by program
> >
> >     (defvar a 1)
> >     (defvar b 2)
> >     (defvar cc 3)
> >
> >     (defun do-something ()
> >       (print (list 'before a b cc))
> >       (setf a 0 b 0 cc 0)
> >       (print (list 'after a b cc)))
> >
> >     (progn
> >       (let ((a a)
> >             (b b)
> >             (cc cc))
> >          (do-something))
> >       (list 'finally a b cc))
> >     prints:
> >
> >     (before 1 2 3)
> >
> >     (after 0 0 0)
> >     --> (finally 1 2 3)
> 
> If you have a lot of global variables you want to preserve like this, or
> in a lot of places, you can write a macro:
> 
>     (defmacro with-saved-variables (variables &rest body)
>       `(let ,(mapcar (lambda (var) (list var var)) variables) 
>          ,@body))
> 
>      (progn
>        (with-saved-variables (a b cc)
>           (do-something))
>        (list 'finally a b cc))
>     prints:
>     (before 1 2 3)
> 
>     (after 0 0 0)
>     --> (finally 1 2 3)
> 
Thank you for your answers. As I'm not familar with macros in elisp, I think your
first answer is good for me.

By the way, you define global variable by (defval). But I always use (setq). What's
the difference bewteen (defval) and (setq)? And with one is recommand to define a
global variable?

     




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

* Re: How to use a symbol and its value to create alist?
       [not found]         ` <mailman.8170.1439355992.904.help-gnu-emacs@gnu.org>
@ 2015-08-12  7:03           ` Pascal J. Bourguignon
  0 siblings, 0 replies; 11+ messages in thread
From: Pascal J. Bourguignon @ 2015-08-12  7:03 UTC (permalink / raw)
  To: help-gnu-emacs

Navy Cheng <navych@126.com> writes:

>> >> I need to push some global variable to a "stack" and pop them later. If
>> >> I don't do like this, the global variables will be changed by program
>> >
>> >     (defvar a 1)
>> >     (defvar b 2)
>> >     (defvar cc 3)
>> >
>> >     (defun do-something ()
>> >       (print (list 'before a b cc))
>> >       (setf a 0 b 0 cc 0)
>> >       (print (list 'after a b cc)))
>> >
>> >     (progn
>> >       (let ((a a)
>> >             (b b)
>> >             (cc cc))
>> >          (do-something))
>> >       (list 'finally a b cc))
>> >     prints:
>> >
>> >     (before 1 2 3)
>> >
>> >     (after 0 0 0)
>> >     --> (finally 1 2 3)
>> 
>> If you have a lot of global variables you want to preserve like this, or
>> in a lot of places, you can write a macro:
>> 
>>     (defmacro with-saved-variables (variables &rest body)
>>       `(let ,(mapcar (lambda (var) (list var var)) variables) 
>>          ,@body))
>> 
>>      (progn
>>        (with-saved-variables (a b cc)
>>           (do-something))
>>        (list 'finally a b cc))
>>     prints:
>>     (before 1 2 3)
>> 
>>     (after 0 0 0)
>>     --> (finally 1 2 3)
>> 
> Thank you for your answers. As I'm not familar with macros in elisp, I think your
> first answer is good for me.
>
> By the way, you define global variable by (defval). But I always use
> (setq). What's the difference bewteen (defval) and (setq)? And with
> one is recommand to define a global variable?

Use emacs!  C-h f defvar RET  C-h f setq RET

It's defvar, not defval.

The difference is that defvar DEFINES a variable, ie, it CREATES it,
while SETQ doesn't create or define it,  it just modifies an EXISTING
variable.  

Also, if the variable already exists, defvar doesn't do anything, so you
can easily reload your lisp files without resetting all the variables.


-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


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

end of thread, other threads:[~2015-08-12  7:03 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-11 13:52 How to use a symbol and its value to create alist? Navy Cheng
2015-08-11 15:21 ` Ian Zimmerman
2015-08-12  1:46   ` Navy Cheng
     [not found]   ` <mailman.8160.1439343998.904.help-gnu-emacs@gnu.org>
2015-08-12  2:12     ` Pascal J. Bourguignon
2015-08-12  2:18       ` Pascal J. Bourguignon
2015-08-12  5:06         ` Navy Cheng
     [not found]         ` <mailman.8170.1439355992.904.help-gnu-emacs@gnu.org>
2015-08-12  7:03           ` Pascal J. Bourguignon
     [not found] <mailman.8135.1439301205.904.help-gnu-emacs@gnu.org>
2015-08-11 14:39 ` Pascal J. Bourguignon
2015-08-12  1:57   ` Navy Cheng
2015-08-12  0:21 ` Barry Margolin
2015-08-12  1:35   ` Navy Cheng

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.