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: [External] : Re: List not getting filled up Date: Mon, 31 Jul 2023 01:08:19 +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="33813"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Yuri Khan , tpeplt , "help-gnu-emacs@gnu.org" To: Drew Adams Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jul 31 03:09:14 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 1qQHPO-0008ah-H5 for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 31 Jul 2023 03:09:14 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQHOp-0007Au-40; Sun, 30 Jul 2023 21:08:39 -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 1qQHOm-0007Ad-GJ for help-gnu-emacs@gnu.org; Sun, 30 Jul 2023 21:08:36 -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 1qQHOi-0007Ha-Ux for help-gnu-emacs@gnu.org; Sun, 30 Jul 2023 21:08:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1690765708; x=1691024908; bh=mhUHTgWwOf80xxsYIaknMCZb5RBZrOoRMvqOEMPwi/c=; 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=cT240hwIHhXy62L/qrl/v4keikFDmPMtn0XxpdkiHRCym7QiWngYZb4HLFKMf6w3A 0a44s7wz7c3TknFpYRpxgHCKeZcVbC54oxEW21qcgCZcR9kK0xq+dURUyzZVF+PQS1 607APtsXzXOdgpK+zzW5XzcvL9Q09dS0SqQBTWvg7vp2wVIYoiP6hQpM/BwDghl6Gq g5zRzLh+IYHpjcnMjoR7tYj1aXVNmkLUZiHemgRrCN8EzW1D9rrWGoyOC//WNRdflU zMdJcq9RkKfNZxz9NVPG7StAxPzVFJVyrS2brcnTW6kHEfBux6bWDyWmmDIV8/F6Jn kiktCYYG/5fjQ== 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:144508 Archived-At: Sent with Proton Mail secure email. ------- Original Message ------- On Monday, July 31st, 2023 at 12:50 PM, Drew Adams = wrote: > > Each time you =E2=80=98push=E2=80=99 that string into your =E2=80=98res= ult=E2=80=99 list, you are > > storing one more reference to the same string object. > >=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-cha= rs=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)) > > (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 be= havior of > > =E2=80=98swap-chars=E2=80=99 is left as an exercise. Hint 1: in your fi= rst > > =E2=80=98swap-chars=E2=80=99 call, you need to arrange for the return v= alue to be > > passed to the recursive invocation of =E2=80=98permute=E2=80=99. Hint 2= : your second > > =E2=80=98swap-chars=E2=80=99 call is only there to undo the modificatio= n made by the > > first call, so it could be avoided by introducing a local variable to > > hold the result of the modification.) >=20 >=20 > Another possibility is to go ahead and change > the string "destructively", over and over, but > create a new string from the result after each > modification, and add that new string to the > list. You can do that with `copy-seq',` format', > or `concat'. >=20 > What's important to understand is what Yuri > explained, including the fact that how you get > get separate strings to add to the list is less > important than understanding that you need to > do that. I'm only pointing out that you need > not forego the use of destructive modification > just because you need to end up with multiple, > separate strings. Have also tried to go through the non-destructive route with this, but the results are not the same. Any idea what might I be doing=20 wrong ? Have removed the first swap-chars and pass it directly to the internal permute. Then I would not need to call swap-chars a=20 second time. This task is quite hard. (defun permute (string i length &optional result) "Generate permutations of STRING using Backtracking Algorithm." (if (null result) (setq result '())) (if (=3D i length) (progn (message "Output %s" string) (push (copy-sequence string) result)) (let ( (j i) ) (while (< j length) (setq result (permute (swap-chars string i j) (1+ i) length result)) (setq j (1+ j))) )) result)