all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Copying a string
@ 2023-07-30 21:07 uzibalqa
  2023-07-30 21:31 ` uzibalqa
  2023-07-31  0:55 ` [External] : " Drew Adams
  0 siblings, 2 replies; 4+ messages in thread
From: uzibalqa @ 2023-07-30 21:07 UTC (permalink / raw)
  To: uzibalqa via Users list for the GNU Emacs text editor


I see many ways to copy a string

(mutant (substring string))
(mutant (copy-sequence string))

Any others ways ? And what should one use routinely ?



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Copying a string
  2023-07-30 21:07 Copying a string uzibalqa
@ 2023-07-30 21:31 ` uzibalqa
  2023-07-31 18:23   ` tpeplt
  2023-07-31  0:55 ` [External] : " Drew Adams
  1 sibling, 1 reply; 4+ messages in thread
From: uzibalqa @ 2023-07-30 21:31 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 Monday, July 31st, 2023 at 9:07 AM, uzibalqa <uzibalqa@proton.me> wrote:


> I see many ways to copy a string
> 
> (mutant (substring string))
> (mutant (copy-sequence string))
> 
> Any others ways ? And what should one use routinely ?

I am swapping characters, I can use as I am doing '(mutant string)'
or as before I had (mutant (copy-sequence string))

I do not think it makes a difference, and that using (copy-sequence string)
would be overkill. Right ? 

(defun swap-chars (string p q)
  "Swap characters at INDEX1 and INDEX2 in STRING."

  (let* ( (char1 (elt string p))
          (char2 (elt string q))
          (mutant string))

    (aset mutant p char2)
    (aset mutant q char1)

    mutant))




^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [External] : Copying a string
  2023-07-30 21:07 Copying a string uzibalqa
  2023-07-30 21:31 ` uzibalqa
@ 2023-07-31  0:55 ` Drew Adams
  1 sibling, 0 replies; 4+ messages in thread
From: Drew Adams @ 2023-07-31  0:55 UTC (permalink / raw)
  To: uzibalqa, uzibalqa via Users list for the GNU Emacs text editor

> I see many ways to copy a string
> (mutant (substring string))
> (mutant (copy-sequence string))
> Any others ways ?

I mentioned these three.  There are no doubt others:

* `copy-seq' (should have said `copy-sequence' -
  `copy-seq' used to be an alias for the CL version)
* `format'
* `concat'

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Copying a string
  2023-07-30 21:31 ` uzibalqa
@ 2023-07-31 18:23   ` tpeplt
  0 siblings, 0 replies; 4+ messages in thread
From: tpeplt @ 2023-07-31 18:23 UTC (permalink / raw)
  To: uzibalqa; +Cc: help-gnu-emacs

uzibalqa <uzibalqa@proton.me> writes:

>
> I do not think it makes a difference, and that using (copy-sequence string)
> would be overkill. Right ? 
>
> (defun swap-chars (string p q)
>   "Swap characters at INDEX1 and INDEX2 in STRING."
>
>   (let* ( (char1 (elt string p))
>           (char2 (elt string q))
>           (mutant string))
>
>     (aset mutant p char2)
>     (aset mutant q char1)
>
>     mutant))

Let’s use Emacs and find out.

1. C-h f eq

> Return t if the two args are the same Lisp object.

2. C-h f equal

> Return t if two Lisp objects have similar structure and contents.
> They must have the same data type.
> Conses are compared by comparing the cars and the cdrs.
> Vectors and strings are compared element by element.

3. Next, comment out the two lines in ‘swap-chars’ that modify the local
variable ‘mutant’:

>     (aset mutant p char2)
>     (aset mutant q char1)

(setq my-str "abc")

(eq my-str (swap-chars my-str 0 2))
=> t

So, ‘my-str’ and the object returned by (swap-chars my-str 0 2) are the
same object.

4. Next, change ‘swap-chars’ to use ‘copy-sequence’, but with the ‘aset’
modifiers still commented out:

(defun swap-chars (string p q)
  "Swap characters at INDEX1 and INDEX2 in STRING."

  (let* ( (char1 (elt string p))
          (char2 (elt string q))
          (mutant (copy-sequence string)))

;;    (aset mutant p char2)
;;    (aset mutant q char1)

    mutant))

(eq my-str (swap-chars my-str 0 2))
=> nil

But the strings will have the same contents until the ‘aset’ modifiers
are restored to the procedure definition.

(equal my-str (swap-chars my-str 0 2))
=> t

Once the ‘aset’ modifiers are restored to the procedure, the definition
can be checked to confirm that it returns the expected value:

(equal "cba" (swap-chars my-str 0 2))
=> t

5. Help -> More Manuals -> Intro to Emacs Lisp

The Intro to Emacs Lisp provides examples with explanations that might
be of value to you.  Here are two sections that might be useful, but the
entire manual should be read to reach the point where it does not have
anything new to teach you, if you are going to be doing routine
programming in Emacs Lisp.

(info "(eintr) Lists diagrammed")
(info "(eintr) Counting function definitions")

--




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-07-31 18:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-30 21:07 Copying a string uzibalqa
2023-07-30 21:31 ` uzibalqa
2023-07-31 18:23   ` tpeplt
2023-07-31  0:55 ` [External] : " Drew Adams

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.