From: Marcin Borkowski <mbork@mbork.pl>
To: uzibalqa <uzibalqa@proton.me>
Cc: uzibalqa via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org>
Subject: Re: Swapping characters in a word inside elisp code
Date: Fri, 28 Jul 2023 22:45:42 +0200 [thread overview]
Message-ID: <87leezpxxl.fsf@mbork.pl> (raw)
In-Reply-To: <P8k4C4w7ifMhsd5hTk6x5tJ29kmPXSCBgSerUJhK3-6RDpqKPKDnb4xHXYWhw9f7Bcu2bANKEDO2VVETJLA3tNO4C6PbsHLUuEKcBhXM-J8=@proton.me>
On 2023-07-28, at 21:44, uzibalqa <uzibalqa@proton.me> wrote:
> Sent with Proton Mail secure email.
>
> ------- Original Message -------
> On Saturday, July 29th, 2023 at 6:03 AM, uzibalqa <uzibalqa@proton.me> wrote:
>
>
>> I have a word and want to swap characters at position i with position j.
>>
>> What would be a good way to do this ? Would I need to change structure
>> (to array, vector or some other thing) ?
>
> I have done it this way, but it fails when p > q
>
> How can I solve this ?
>
> (defun cswap (wstr p q)
> "Replace characters in WSTR at positions P and Q."
>
> (let ( (char1 (elt wstr p))
> (char2 (elt wstr q)) )
>
> (if (and char1 char2)
> (concat (substring wstr 0 p)
> (char-to-string char2)
> (substring wstr (+ p 1) q)
> (char-to-string char1)
> (substring wstr (+ q 1)))) ))
I'd probably use `format', not `concat'. Alternatively, it might be
closer to idiomatic Elisp to put the word in a buffer and perform the
swap using editing operations (though this makes more sense for larger
things like words in a sentence, and probably less so for characters in
a word).
Also, if your code works only for p < q (which I believe you without
checking myself since it's about 22:45 here and my brain doesn't
function very well), why not include a check for q < p at the beginning
and call the same function recursively with p and q swapped?
Hth,
--
Marcin Borkowski
http://mbork.pl
next prev parent reply other threads:[~2023-07-28 20:45 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-28 18:03 Swapping characters in a word inside elisp code uzibalqa
2023-07-28 19:44 ` uzibalqa
2023-07-28 20:45 ` Marcin Borkowski [this message]
2023-07-28 20:32 ` [External] : " Drew Adams
2023-07-28 20:45 ` uzibalqa
2023-07-28 20:48 ` uzibalqa
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87leezpxxl.fsf@mbork.pl \
--to=mbork@mbork.pl \
--cc=help-gnu-emacs@gnu.org \
--cc=uzibalqa@proton.me \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).