From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: uzibalqa Newsgroups: gmane.emacs.help Subject: Re: List not getting filled up Date: Mon, 31 Jul 2023 00:06:41 +0000 Message-ID: References: <87sf95lbic.fsf@gmail.com> <6ttLn1Fro5yCAyltr_mXoHE3fMaMN2hNn9eaO2vmngvz4XJJkDo9hzCCwQBeB8PMON2qjUONuqKtYsJw7FGOY7HxlTE-im3ZPVzHiZrydU4=@proton.me> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33202"; mail-complaints-to="usenet@ciao.gmane.io" Cc: tpeplt , help-gnu-emacs@gnu.org To: Yuri Khan Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jul 31 02:07:51 2023 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 1qQGRz-0008Q5-5U for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 31 Jul 2023 02:07:51 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQGRA-0005dC-7N; Sun, 30 Jul 2023 20:07:00 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQGR4-0005ch-8V for help-gnu-emacs@gnu.org; Sun, 30 Jul 2023 20:06:57 -0400 Original-Received: from mail-40140.protonmail.ch ([185.70.40.140]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQGR1-0004fY-HE for help-gnu-emacs@gnu.org; Sun, 30 Jul 2023 20:06:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1690762008; x=1691021208; bh=2/GH1AKEGHh42agZyoqbi/l3Yl4eGbdTg+N0m/CR0KQ=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=cQeEyI9VCMJSe6yPfJBCH6q8JBQlQGr0DeasttqaKRoT0zW+mMMvUoCtOxnunubVs bS8F18B0MBn0QRgbc+0ig1myEQNuwaVcmj/XOeG6Dsx0t1rIjajxXh/idQyBiRyYGS bK+Jd6Sioa/SuMWS3+llxHYdS9Ds3E2EiGWKKKDGZc66T34k+EhqQ1TS+OEeK7nrt3 ETTwVXAh7T6hIO8w9bqZQK3I0SroEiJYqKgkqiOWFBx7uFJhhB13yaw354SHCI46vg HVMgAr3wz4Fe+LMWQGZM+1MXnOa+aDIB16WcmuKrY7u8hjBthPY8sNBiXX/MUSx7qV yzkBEICVngWRA== In-Reply-To: Feedback-ID: 52887082:user:proton Received-SPF: pass client-ip=185.70.40.140; envelope-from=uzibalqa@proton.me; helo=mail-40140.protonmail.ch X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.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, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 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-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:144505 Archived-At: ------- Original Message ------- On Monday, July 31st, 2023 at 5:42 AM, Yuri Khan wr= ote: > On Sun, 30 Jul 2023 at 22:11, uzibalqa uzibalqa@proton.me wrote: >=20 > > Use this as a test > >=20 > > (setq collection '()) > > (permute "abc" 0 3 collection) > > (message "%s" collection) > >=20 > > I should get > >=20 > > collection (abc acb bac bca cba cab) > >=20 > > But I get > >=20 > > Collection (abc abc abc abc abc abc) >=20 >=20 > You have a function that builds a list of strings. >=20 > Initially, you have an initial string value, "abc". Along the way, you > modify its contents, but ultimately there is only one string instance > in your program. >=20 > Each time you =E2=80=98push=E2=80=99 that string into your =E2=80=98resul= t=E2=80=99 list, you are > storing one more reference to the same string object. In > box-and-pointer diagrams: >=20 > =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=AC=E2=94=80=E2=94=80=E2=94= =80=E2=94=90 =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=AC=E2=94=80= =E2=94=80=E2=94=80=E2=94=90 =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94= =AC=E2=94=80=E2=94=80=E2=94=80=E2=94=90 =E2=94=8C=E2=94=80=E2=94=80= =E2=94=80=E2=94=AC=E2=94=80=E2=94=80=E2=94=80=E2=94=90 =E2=94=8C=E2=94= =80=E2=94=80=E2=94=80=E2=94=AC=E2=94=80=E2=94=80=E2=94=80=E2=94=90 = =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=AC=E2=94=80=E2=94=80=E2=94=80= =E2=94=90 > =E2=94=82 * =E2=94=82 *=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=86=92= =E2=94=82 * =E2=94=82 *=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=86=92= =E2=94=82 * =E2=94=82 *=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=86=92= =E2=94=82 * =E2=94=82 *=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=86=92= =E2=94=82 * =E2=94=82 *=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=86=92= =E2=94=82 * =E2=94=82nil=E2=94=82 > =E2=94=94=E2=94=80=E2=94=BC=E2=94=80=E2=94=B4=E2=94=80=E2=94=80=E2=94= =80=E2=94=98 =E2=94=94=E2=94=80=E2=94=BC=E2=94=80=E2=94=B4=E2=94=80= =E2=94=80=E2=94=80=E2=94=98 =E2=94=94=E2=94=80=E2=94=BC=E2=94=80=E2=94= =B4=E2=94=80=E2=94=80=E2=94=80=E2=94=98 =E2=94=94=E2=94=80=E2=94=BC= =E2=94=80=E2=94=B4=E2=94=80=E2=94=80=E2=94=80=E2=94=98 =E2=94=94=E2=94= =80=E2=94=BC=E2=94=80=E2=94=B4=E2=94=80=E2=94=80=E2=94=80=E2=94=98 = =E2=94=94=E2=94=80=E2=94=BC=E2=94=80=E2=94=B4=E2=94=80=E2=94=80=E2=94=80= =E2=94=98 > =E2=94=82=E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=98 =E2=94=82 =E2=94=82 = =E2=94=82 =E2=94=82 > =E2=94=82=E2=94=82=E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=98 =E2=94=82 =E2=94=82 =E2=94=82 > =E2=94=82=E2=94=82=E2=94=82=E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=98 =E2=94=82 =E2=94=82 > =E2=94=82=E2=94=82=E2=94=82=E2=94=82=E2=94=8C=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=98 =E2=94=82 > =E2=94=82=E2=94=82=E2=94=82=E2=94=82=E2=94=82=E2=94=8C=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=98 > =E2=86=93=E2=86=93=E2=86=93=E2=86=93=E2=86=93=E2=86=93 > =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=90 > =E2=94=82 (mutable string) =E2=94=82 > =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=98 >=20 > This explains why you get a list that looks like it contains 6 > identical strings =E2=80=94 they are the same string. >=20 > Instead, you should be avoiding in-place string mutation and building > new string instances as you go, perhaps by redefining =E2=80=98swap-chars= =E2=80=99 to > be non-destructive: >=20 > (defun swap-chars (string p q) > "Swap chars in STRING at positions P and Q." > (cond > ((=3D p q) string) > ((> p q) (swap-chars string q p)) >=20 > (t (concat > (substring string 0 p) > (substring string q (1+ q)) > (substring string (1+ p) q) > (substring string p (1+ p)) > (substring string (1+ q)))))) >=20 > (This is likely not the fastest way to build a string with two > characters swapped. Do not let that distract you from the goal. Your > first goal is to get your program working correctly. When and if that > happens, and when and if your working solution is deemed too slow, > only then you start optimizing.) >=20 > (Adjusting the =E2=80=98permute=E2=80=99 function to non-destructive beha= vior of > =E2=80=98swap-chars=E2=80=99 is left as an exercise. Hint 1: in your firs= t > =E2=80=98swap-chars=E2=80=99 call, you need to arrange for the return val= ue to be > passed to the recursive invocation of =E2=80=98permute=E2=80=99.=20 Have done a non-destructive swap-chars which I use directly as follows (setq result (permute (swap-chars string i j) (1+ i) length result)) > Hint 2: your second > =E2=80=98swap-chars=E2=80=99 call is only there to undo the modification = made by the > first call, so it could be avoided by introducing a local variable to > hold the result of the modification.) For your second hint, I am unsure what to store. Would I store the origina= l=20 string passed as an argument to the function as follows. Then what would I= =20 do with copy of the string that war not swapped ? (let ( (j i) (str string) ) (while (< j length) (setq result (permute (swap-chars string i j) (1+ i) length result)) (setq j (1+ j))) ))