From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: using setq to create lists based on other lists... Date: Sun, 02 Dec 2018 12:02:50 -0500 Message-ID: References: <5C1642CD-F485-47CF-88B9-41D7960DDE80@gmail.com> <6641A86F-1E33-44BA-8CEE-625F3213D9C4@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1543770271 23353 195.159.176.226 (2 Dec 2018 17:04:31 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 2 Dec 2018 17:04:31 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Dec 02 18:04:27 2018 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gTVAP-0005uO-Dy for geh-help-gnu-emacs@m.gmane.org; Sun, 02 Dec 2018 18:04:25 +0100 Original-Received: from localhost ([::1]:45248 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gTVCV-0003aa-Nr for geh-help-gnu-emacs@m.gmane.org; Sun, 02 Dec 2018 12:06:35 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52537) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gTVBx-00036O-1A for help-gnu-emacs@gnu.org; Sun, 02 Dec 2018 12:06:06 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gTV96-0000WZ-7b for help-gnu-emacs@gnu.org; Sun, 02 Dec 2018 12:03:09 -0500 Original-Received: from [195.159.176.226] (port=52317 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gTV95-0000TD-VJ for help-gnu-emacs@gnu.org; Sun, 02 Dec 2018 12:03:04 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1gTV6u-0001G9-HP for help-gnu-emacs@gnu.org; Sun, 02 Dec 2018 18:00:48 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 43 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:AGjlsryEFKAnS+g/p1SG/LJEAvo= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 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" Xref: news.gmane.org gmane.emacs.help:118836 Archived-At: > I see. Eventually I used copy-tree instead to initialize the new lists and > then modified them separately with setcar. So you're back to using `setcar` :-( > Just out of curiosity, let me post what I did. There are probably > better ways to do it, but that's the best I could come up with > today. I'd love to be able to think in terms closer to what elisp > allows though. Those date/time thingies are indeed rather annoying to construct. copy-sequence + in-place modification is probably the best you can use, indeed :-( > (setq now (decode-time (float-time)) > myDateLastMonth (copy-tree now) > myDateThisMonth (copy-tree now) > myDateNextMonth (copy-tree now) > now (encode-time now 'integer)) You're using `setq` on vars you haven't declared/defined yet! And theses aren't "trees" but lists/sequences, so better use `copy-sequence` which is also more efficient: (let* ((decoded-now (decode-time (float-time))) (myDateLastMonth (copy-tree now)) (myDateThisMonth (copy-tree now)) (myDateNextMonth (copy-tree now)) (encoded-now (encode-time now 'integer))) I recommend you put `-*- lexical-binding:t -*-` somewhere on the first line of your file, and that you `M-x byte-compile RET` your file so Emacs can help you catch some of those issues. > ;; create the data for last month > (setcar (cdr (cdr (cdr myDateLastMonth))) myDay) Better write this as (setf (nth 3 myDateLastMonth) myDay) It's bad enough that the fields aren't named so you have to refer to them by position, but having to count `cdr`s is really annoying IMO. -- Stefan