[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [External] : Swapping characters in a word inside elisp code
From: |
uzibalqa |
Subject: |
RE: [External] : Swapping characters in a word inside elisp code |
Date: |
Fri, 28 Jul 2023 20:48:42 +0000 |
------- 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)))