* Swapping characters in a word inside elisp code
@ 2023-07-28 18:03 uzibalqa
2023-07-28 19:44 ` uzibalqa
2023-07-28 20:32 ` [External] : " Drew Adams
0 siblings, 2 replies; 6+ messages in thread
From: uzibalqa @ 2023-07-28 18:03 UTC (permalink / raw)
To: uzibalqa via Users list for the GNU Emacs text editor
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) ?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Swapping characters in a word inside elisp code
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
2023-07-28 20:32 ` [External] : " Drew Adams
1 sibling, 1 reply; 6+ messages in thread
From: uzibalqa @ 2023-07-28 19:44 UTC (permalink / raw)
To: uzibalqa; +Cc: uzibalqa via Users list for the GNU Emacs text editor
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)))) ))
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Swapping characters in a word inside elisp code
2023-07-28 19:44 ` uzibalqa
@ 2023-07-28 20:45 ` Marcin Borkowski
0 siblings, 0 replies; 6+ messages in thread
From: Marcin Borkowski @ 2023-07-28 20:45 UTC (permalink / raw)
To: uzibalqa; +Cc: uzibalqa via Users list for the GNU Emacs text editor
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [External] : Swapping characters in a word inside elisp code
2023-07-28 18:03 Swapping characters in a word inside elisp code uzibalqa
2023-07-28 19:44 ` uzibalqa
@ 2023-07-28 20:32 ` Drew Adams
2023-07-28 20:45 ` uzibalqa
1 sibling, 1 reply; 6+ messages in thread
From: Drew Adams @ 2023-07-28 20:32 UTC (permalink / raw)
To: uzibalqa, uzibalqa via Users list for the GNU Emacs text editor
> 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) ?
(Homework?)
Depends what you mean by swap chars in a word.
And whether your word is represented by a string,
a vector, a list... And how you want the result:
in a separate string, vector,... or in the same
one, modified.
(Again, you don't make clear what you want.)
Here's one way to swap chars in a string
destructively:
(defun cswap (string p q)
"Swap chars in STRING at positions P and Q.
This is a destructive operation."
(aset string p (prog1 (aref string q)
(aset string q (aref string p))))
string)
(setq s1 "123456789")
(cswap s1 3 5) ; s1 = "123654789"
; ^ ^
`prog1' is often used to swap things.
A more typical use is swapping values
of two variables:
(setq start (prog1 end (setq end start)))
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [External] : Swapping characters in a word inside elisp code
2023-07-28 20:32 ` [External] : " Drew Adams
@ 2023-07-28 20:45 ` uzibalqa
2023-07-28 20:48 ` uzibalqa
0 siblings, 1 reply; 6+ messages in thread
From: uzibalqa @ 2023-07-28 20:45 UTC (permalink / raw)
To: Drew Adams; +Cc: uzibalqa via Users list for the GNU Emacs text editor
------- Original Message -------
On Saturday, July 29th, 2023 at 8:32 AM, Drew Adams <drew.adams@oracle.com> 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) ?
>
>
> (Homework?)
>
> Depends what you mean by swap chars in a word.
> And whether your word is represented by a string,
> a vector, a list... And how you want the result:
> in a separate string, vector,... or in the same
> one, modified.
I have a word and want to generate permutations of it.
And I need the ability to swap two characters at positions
i and j in word.
Just a toy project that does scrabble.
> (Again, you don't make clear what you want.)
>
> Here's one way to swap chars in a string
> destructively:
>
> (defun cswap (string p q)
> "Swap chars in STRING at positions P and Q.
> This is a destructive operation."
> (aset string p (prog1 (aref string q)
> (aset string q (aref string p))))
> string)
>
> (setq s1 "123456789")
> (cswap s1 3 5) ; s1 = "123654789"
> ; ^ ^
>
> `prog1' is often used to swap things.
> A more typical use is swapping values
> of two variables:
>
> (setq start (prog1 end (setq end start)))
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [External] : Swapping characters in a word inside elisp code
2023-07-28 20:45 ` uzibalqa
@ 2023-07-28 20:48 ` uzibalqa
0 siblings, 0 replies; 6+ messages in thread
From: uzibalqa @ 2023-07-28 20:48 UTC (permalink / raw)
To: Drew Adams; +Cc: uzibalqa via Users list for the GNU Emacs text editor
------- Original Message -------
On Saturday, July 29th, 2023 at 8:45 AM, uzibalqa <uzibalqa@proton.me> wrote:
> ------- Original Message -------
> On Saturday, July 29th, 2023 at 8:32 AM, Drew Adams drew.adams@oracle.com 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) ?
> >
> > (Homework?)
> >
> > Depends what you mean by swap chars in a word.
> > And whether your word is represented by a string,
> > a vector, a list... And how you want the result:
> > in a separate string, vector,... or in the same
> > one, modified.
>
>
> I have a word and want to generate permutations of it.
> And I need the ability to swap two characters at positions
> i and j in word.
>
> Just a toy project that does scrabble.
>
> > (Again, you don't make clear what you want.)
> >
> > Here's one way to swap chars in a string
> > destructively:
> >
> > (defun cswap (string p q)
> > "Swap chars in STRING at positions P and Q.
> > This is a destructive operation."
> > (aset string p (prog1 (aref string q)
> > (aset string q (aref string p))))
> > string)
I had made a function to do it but the changes were local (temporary)
because I was using setq.
> > (setq s1 "123456789")
> > (cswap s1 3 5) ; s1 = "123654789"
> > ; ^ ^
> >
> > `prog1' is often used to swap things.
> > A more typical use is swapping values
> > of two variables:
> >
> > (setq start (prog1 end (setq end start)))
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2023-07-28 20:48 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2023-07-28 20:32 ` [External] : " Drew Adams
2023-07-28 20:45 ` uzibalqa
2023-07-28 20:48 ` uzibalqa
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).