unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Rodrigo Morales <moralesrodrigo1100@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: [Elisp][Question] How to modify a list by index while preserving value outside of scope?
Date: Sun, 20 Aug 2023 03:46:00 +0000	[thread overview]
Message-ID: <CAGxMbPZkAutH3KDZRth0_ii9H5FTsAdS97_JRsqeXwd=iV556Q@mail.gmail.com> (raw)

----
(defun my/f (foo bar)
  (princ (format "(foo: %s) (bar: %s)\n" foo bar))
  (cond
   (foo (setf (nth 0 bar) "100"))
   (t (my/f "apples" bar)
      (my/f "bananas" bar))))

(my/f nil (list 123))
----

---
(foo: nil) (bar: (123))
(foo: apples) (bar: (123))
(foo: bananas) (bar: (100))
---

I have some questions:

+ The second time my/f function is called (i.e. when "apples" is
  passed), `bar' equals `123'. The third time `my/f' is called
  (i.e. when "bananas" is passed), `bar' has a different value. We can
  conclude that the modification to `bar' in the second call affected
  the third call. How is this possible? `bar' is an argument of `my/f',
  as far as I'm concerned, any modification to a variable that is a
  function parameter only affects the scope of the function.
+ This is a minimal working example, in reality, the code I'm writing is
  more complex. In the code that I'm writing, `bar' is a list and I want
  to modify some of their elements by index. The only way I know is by
  using `(setf (nth index my-list) new-value)'. However, using this
  method seems to changes the value of the variable outside of the
  function call. Are there any other ways to modify a list by index
  without affecting its value outside of the function call?



             reply	other threads:[~2023-08-20  3:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-20  3:46 Rodrigo Morales [this message]
2023-08-20  4:08 ` [Elisp][Question] How to modify a list by index while preserving value outside of scope? Rodrigo Morales
2023-08-20  5:38 ` Marcin Borkowski
2023-08-20 21:45   ` [External] : " Drew Adams

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='CAGxMbPZkAutH3KDZRth0_ii9H5FTsAdS97_JRsqeXwd=iV556Q@mail.gmail.com' \
    --to=moralesrodrigo1100@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /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).