all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: Yuri Khan <yuri.v.khan@gmail.com>, uzibalqa <uzibalqa@proton.me>
Cc: tpeplt <tpeplt@gmail.com>,
	"help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: RE: [External] : Re: List not getting filled up
Date: Mon, 31 Jul 2023 00:50:41 +0000	[thread overview]
Message-ID: <SJ0PR10MB5488956B01F5FBB35EB8E2ACF305A@SJ0PR10MB5488.namprd10.prod.outlook.com> (raw)
In-Reply-To: <CAP_d_8WBmMCjru0oOXGgOWjpaAMirGzVSULDscuigfs+rwyAQQ@mail.gmail.com>

> Each time you ‘push’ that string into your ‘result’ list, you are
> storing one more reference to the same string object. 
> 
> This explains why you get a list that looks like it contains 6
> identical strings — they are *the same* string.
> 
> Instead, you should be avoiding in-place string mutation and building
> new string instances as you go, perhaps by redefining ‘swap-chars’ to
> be non-destructive:
> 
>     (defun swap-chars (string p q)
>       "Swap chars in STRING at positions P and Q."
>       (cond
>        ((= 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))))))
> 
> (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.)
> 
> (Adjusting the ‘permute’ function to non-destructive behavior of
> ‘swap-chars’ is left as an exercise. Hint 1: in your first
> ‘swap-chars’ call, you need to arrange for the return value to be
> passed to the recursive invocation of ‘permute’. Hint 2: your second
> ‘swap-chars’ 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.)

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'.

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.

  parent reply	other threads:[~2023-07-31  0:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-28 23:21 List not getting filled up uzibalqa
2023-07-29 11:19 ` Emanuel Berg
2023-07-30 14:28 ` tpeplt
2023-07-30 14:47   ` uzibalqa
2023-07-30 17:42     ` Yuri Khan
2023-07-31  0:06       ` uzibalqa
2023-07-31  8:55         ` Yuri Khan
2023-07-31  0:50       ` Drew Adams [this message]
2023-07-31  1:08         ` [External] : " uzibalqa
2023-07-31  1:29           ` Drew Adams
2023-07-31  1:34             ` uzibalqa
2023-07-31  1:40               ` Drew Adams
2023-07-31  2:03                 ` uzibalqa
2023-07-31  2:59                   ` Heime

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=SJ0PR10MB5488956B01F5FBB35EB8E2ACF305A@SJ0PR10MB5488.namprd10.prod.outlook.com \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=tpeplt@gmail.com \
    --cc=uzibalqa@proton.me \
    --cc=yuri.v.khan@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.