From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Newsgroups: gmane.emacs.help Subject: Re: Appending lists Date: Tue, 15 Jun 2021 11:18:34 +0200 Message-ID: <20210615091834.GB24886@tuxteam.de> References: <4tl1yvylvg1fxx5eefjs9mnk.1623688568572@email.android.com> <87o8c8l32h.fsf@posteo.net> <87zgvs2bup.fsf@zoho.eu> <87y2bby1kr.fsf@zoho.eu> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="z6Eq5LdranGa6ru8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34938"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/1.5.21 (2010-09-15) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jun 15 11:19:21 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lt5E9-0008v6-Fx for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 15 Jun 2021 11:19:21 +0200 Original-Received: from localhost ([::1]:46360 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lt5E8-0000ih-86 for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 15 Jun 2021 05:19:20 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52780) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lt5DZ-0000i0-V5 for help-gnu-emacs@gnu.org; Tue, 15 Jun 2021 05:18:45 -0400 Original-Received: from mail.tuxteam.de ([5.199.139.25]:57970) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.90_1) (envelope-from ) id 1lt5DX-0008LG-Gf for help-gnu-emacs@gnu.org; Tue, 15 Jun 2021 05:18:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=mail; h=From:In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:To:Date; bh=slYE6xNiugK8FzuyXEGQ0ce8nAaGSxdynJ4/w5SUSPI=; b=pk5ZgKOz9REuJJsM9fYhMENnzmWhiQ0rL7oSBpYwlLinh9wvVj36ONgh2zW9Vgkiy1lnsSYhlsClliX5+rfJwp1Vn9ck4yTne43m15Xk4tFyvX5i0Jh+sAMYrlsvS17Dj8jZzXEiyYvFxD51W+4EBrgrHVrZe7V/r/79gh57quXdQqaviFQp29I2w0Xpsv0ZHO2lp3mQapte2A3oJ9jzXFx0Ybn0GCh/3vOTBpvRUSigZNjzTVSVl1dRIM/J93V1tLPccDqAubsUIoGt+WFvar2NWh8ki7DPIAwuOJCkGvwshw/iws6if4dd1pwOTTNz9oyIcrP+xrPzyixfDIm92w==; Original-Received: from tomas by mail.tuxteam.de with local (Exim 4.80) (envelope-from ) id 1lt5DO-00084l-Pu for help-gnu-emacs@gnu.org; Tue, 15 Jun 2021 11:18:34 +0200 Content-Disposition: inline In-Reply-To: <87y2bby1kr.fsf@zoho.eu> Received-SPF: pass client-ip=5.199.139.25; envelope-from=tomas@tuxteam.de; helo=mail.tuxteam.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:130865 Archived-At: --z6Eq5LdranGa6ru8 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Jun 15, 2021 at 10:27:48AM +0200, Emanuel Berg via Users list for t= he GNU Emacs text editor wrote: > > Also check out `nconc' > > > > (nconc '(1 2 3) '(4 5) '(6)) ; (1 2 3 4 5 6) - if this works for you, u= se it > > (nconc '(1 2 3) '(4 5) 6 ) ; (1 2 3 4 5 . 6) - see, last "list" isn't= a list! > > > > There was a trick with nconc, you could reverse a list in > > constant time, right? I think that was it but that's all > > I remember. >=20 > Probably that wasn't `nconc' but `nreverse', I think! >=20 > (nreverse '(flip six three hole)) ; (hole three six flip) >=20 > Yeah, and it didn't take very long either... >=20 > Here it says >=20 > Don't use nreverse on quoted list literals; it is undefined > behavior and may behave in surprising ways, since it is > de facto self-modifying code. [1] Thing is, '(flip six three hole) is a "constant". It is known at compile time. The compiler takes its freedom to assume that it won't be changed. It even might end up in a chunk of read-only memory if/when the code makes it to a dumped image. Good luck flipping pointers in cons cells in read-only memory :) I'd go even further with the warning: when using nreverse (and friends), make sure there are no other users of (part of) your data or be prepared for fun surprises:. setq thing (copy-sequence '(one two three four five six))) (setq thang (cddr thing)) =20 thang =3D> (three four five six) =20 (nreverse thing) =3D> (six five four three two one) =20 thing =3D> (one) ; the above isn't the surprise I was after, just a reminder to do ; (setq thing (nreverse thing)) -- nreverse won't change your variable ; in-place! The result seems weird, but do your box-and-pointer ; homework, and you'll get it. =20 thang =3D> (three two one) ; now this is what I was after. Who the heck "changed my variable!?" ; Who is General Failure and why is he reading my disk? Hours of fun. Especially when debugging some more involved code :) Cheers - t --z6Eq5LdranGa6ru8 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAmDIcGoACgkQBcgs9XrR2kb7+QCfbkb90ivWYr3Q05/PLKey+P4i KVAAn1EhGERkimTeqmmcK/eUyHXK9rrg =RmaU -----END PGP SIGNATURE----- --z6Eq5LdranGa6ru8--