From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Navy Cheng Newsgroups: gmane.emacs.help Subject: Re: How to use a symbol and its value to create alist? Date: Wed, 12 Aug 2015 13:06:12 +0800 Message-ID: <20150812050612.GA22588@debian> References: <20150811135254.GA20200@debian> <20150811151533.17219.21BE0C0D@ahiker.mooo.com> <874mk5fe5c.fsf@kuiper.lan.informatimago.com> <87zj1xdzc3.fsf@kuiper.lan.informatimago.com> Reply-To: Navy Cheng NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1439356009 17173 80.91.229.3 (12 Aug 2015 05:06:49 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 12 Aug 2015 05:06:49 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Aug 12 07:06:41 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZPOFN-00048Z-5S for geh-help-gnu-emacs@m.gmane.org; Wed, 12 Aug 2015 07:06:41 +0200 Original-Received: from localhost ([::1]:36884 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZPOFM-0005s7-5R for geh-help-gnu-emacs@m.gmane.org; Wed, 12 Aug 2015 01:06:40 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54324) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZPOFC-0005s2-Go for help-gnu-emacs@gnu.org; Wed, 12 Aug 2015 01:06:31 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZPOF9-0003dy-5w for help-gnu-emacs@gnu.org; Wed, 12 Aug 2015 01:06:30 -0400 Original-Received: from m15-114.126.com ([220.181.15.114]:54875) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZPOF8-0003bc-00 for help-gnu-emacs@gnu.org; Wed, 12 Aug 2015 01:06:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=Date:From:Subject:Message-ID:MIME-Version; bh=RzWy/ cTO5myJMa4S+lQqOZqoAubMR2ORaPOc/mRxkOg=; b=CeivXGcP6TwtQWQwf6zyT dLenBQT69WF7HAossqqi4xzwDBT4dL5LvHSwz2QeNTnRFQz0yKGBU7XGEpOQMiRp 5YRgnYMxxps+ssr62Alf0uw/NPQxZqZ2M1R5W1EOo9WEKZCaHmCFQvsxI3Snls6v GY42srxTfrkQ2Q0IthbZCY= Original-Received: from localhost (unknown [140.207.196.3]) by smtp7 (Coremail) with SMTP id DsmowADH4YxF1MpV1fQkAA--.2769S3; Wed, 12 Aug 2015 13:06:13 +0800 (CST) Content-Disposition: inline In-Reply-To: <87zj1xdzc3.fsf@kuiper.lan.informatimago.com> User-Agent: Mutt/1.5.23 (2014-03-12) X-CM-TRANSID: DsmowADH4YxF1MpV1fQkAA--.2769S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7Cr1ftFW8Ary8Gr15uF4rXwb_yoW8Cr17p3 4UZF1YkF4DZFyj9w1qqF1Dtr1jqw10gFyxW34kAa4qv3ZYq3srWr45A3yrua47uFnrZr1x Xay2vF9xX3yYkFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jrOzsUUUUU= X-Originating-IP: [140.207.196.3] X-CM-SenderInfo: 5qdy5ubk6rjloofrz/1tbiOwZUDVWfktJGeAAAsQ X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 220.181.15.114 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:106491 Archived-At: On Wed, Aug 12, 2015 at 04:18:04AM +0200, Pascal J. Bourguignon wrote: > "Pascal J. Bourguignon" writes: > > > Navy Cheng 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?