From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Drew Adams Newsgroups: gmane.emacs.help Subject: RE: alist-get in Emacs 24? Date: Wed, 7 Oct 2015 13:45:11 -0700 (PDT) Message-ID: <5830aeae-99de-461a-9414-e746edba9211@default> References: <871tddvl08.fsf@mbork.pl> <877fn5hgg8.fsf@web.de> <87k2qyij75.fsf@mbork.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1444250784 19195 80.91.229.3 (7 Oct 2015 20:46:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 7 Oct 2015 20:46:24 +0000 (UTC) To: Marcin Borkowski , help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Oct 07 22:46:13 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 1Zjvb7-0002Qv-Cg for geh-help-gnu-emacs@m.gmane.org; Wed, 07 Oct 2015 22:46:01 +0200 Original-Received: from localhost ([::1]:59383 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zjvb1-0006wz-L2 for geh-help-gnu-emacs@m.gmane.org; Wed, 07 Oct 2015 16:45:55 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49825) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjvaT-0006vY-9u for help-gnu-emacs@gnu.org; Wed, 07 Oct 2015 16:45:22 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZjvaO-0002pC-6J for help-gnu-emacs@gnu.org; Wed, 07 Oct 2015 16:45:21 -0400 Original-Received: from userp1040.oracle.com ([156.151.31.81]:20346) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjvaN-0002ou-VF for help-gnu-emacs@gnu.org; Wed, 07 Oct 2015 16:45:16 -0400 Original-Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id t97KjDhS023050 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 7 Oct 2015 20:45:14 GMT Original-Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.13.8/8.13.8) with ESMTP id t97KjCVZ014510 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Wed, 7 Oct 2015 20:45:13 GMT Original-Received: from abhmp0004.oracle.com (abhmp0004.oracle.com [141.146.116.10]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id t97KjCLN011760; Wed, 7 Oct 2015 20:45:12 GMT In-Reply-To: <87k2qyij75.fsf@mbork.pl> X-Priority: 3 X-Mailer: Oracle Beehive Extensions for Outlook 2.0.1.9 (901082) [OL 12.0.6691.5000 (x86)] X-Source-IP: aserv0021.oracle.com [141.146.126.233] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 156.151.31.81 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:107547 Archived-At: > I have to pass the alist variable name as a symbol. (This is needed > because I might want to use it to introduce a new key-value pair > into the list.) Does anyone have any suggestion for enhancing my > solution? >=20 > (defun my-set-alist-value (key alist value) > "Set the value corresponding to KEY in ALIST to VALUE. > Note: ALIST should be a symbol. This is morally equivalent to > `(setf (alist-get key (symbol-value alist)) value)', > but works in older Emacsen." > (let ((pair (assoc key (symbol-value alist)))) > (if pair > =09(setcdr pair value) > (set alist (acons key value (symbol-value alist)))))) You say that ALIST must be a symbol here, but I don't see why. Why not just modify the alist structure (i.e., the alist) itself? Why repoint the variable to the result inside the set function? If you modify the structure in place, and you return the new alist, then you can always do this to ensure that the variable is up-to-date: (setq THE-VAR (my-set-alist-value key alist value)) That's typically the way destructive operations are done. It is why you need to do (setq foo (delete 42 foo)).