* [Elisp][Question] How to modify a list by index while preserving value outside of scope?
@ 2023-08-20 3:46 Rodrigo Morales
2023-08-20 4:08 ` Rodrigo Morales
2023-08-20 5:38 ` Marcin Borkowski
0 siblings, 2 replies; 4+ messages in thread
From: Rodrigo Morales @ 2023-08-20 3:46 UTC (permalink / raw)
To: help-gnu-emacs
----
(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?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Elisp][Question] How to modify a list by index while preserving value outside of scope?
2023-08-20 3:46 [Elisp][Question] How to modify a list by index while preserving value outside of scope? Rodrigo Morales
@ 2023-08-20 4:08 ` Rodrigo Morales
2023-08-20 5:38 ` Marcin Borkowski
1 sibling, 0 replies; 4+ messages in thread
From: Rodrigo Morales @ 2023-08-20 4:08 UTC (permalink / raw)
To: help-gnu-emacs
Here's a simpler example.
---
(defun my/f2 (foo)
(setf (nth 0 foo) 12))
(defun my/f1 (foo)
(princ (format "%s\n" foo))
(my/f2 foo)
(princ (format "%s\n" foo)))
(let ((items (list 1 2 3)))
(my/f1 items))
---
---
(1 2 3)
(12 2 3)
---
Today I learned that `setf' can be used inside functions to change the
value of lists and see those changes outside of the function. I
wrongly believed that whenever function parameters were passed by copy
and there was no way to see the changes reflected outside of the
function (i.e. the only way would be to return a value).
Relevant information on this topic is appreciated.
On Sun, 20 Aug 2023 at 03:46, Rodrigo Morales
<moralesrodrigo1100@gmail.com> wrote:
>
> ----
> (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?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Elisp][Question] How to modify a list by index while preserving value outside of scope?
2023-08-20 3:46 [Elisp][Question] How to modify a list by index while preserving value outside of scope? Rodrigo Morales
2023-08-20 4:08 ` Rodrigo Morales
@ 2023-08-20 5:38 ` Marcin Borkowski
2023-08-20 21:45 ` [External] : " Drew Adams
1 sibling, 1 reply; 4+ messages in thread
From: Marcin Borkowski @ 2023-08-20 5:38 UTC (permalink / raw)
To: Rodrigo Morales; +Cc: help-gnu-emacs
On 2023-08-20, at 05:46, Rodrigo Morales <moralesrodrigo1100@gmail.com> wrote:
> ----
> (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?
Welcome to the rabbit hole of conses, lists etc.
In short: a "list" is in fact a "pointer", i.e., an "adress in memory"
where the first cons of that list lives. (Read the chapter "How Lists
are Implemented" in the Elisp Intro for more info.)
If you want to work on a "local" version of a list, you can copy that
list yourself. `append' is one way to do it; `cl-copy-list' is another.
Hth,
--
Marcin Borkowski
http://mbork.pl
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: [External] : Re: [Elisp][Question] How to modify a list by index while preserving value outside of scope?
2023-08-20 5:38 ` Marcin Borkowski
@ 2023-08-20 21:45 ` Drew Adams
0 siblings, 0 replies; 4+ messages in thread
From: Drew Adams @ 2023-08-20 21:45 UTC (permalink / raw)
To: Marcin Borkowski, Rodrigo Morales; +Cc: help-gnu-emacs@gnu.org
> In short: a "list" is in fact a "pointer", i.e., an "adress in memory"
> where the first cons of that list lives.
A NON-empty list is a cons, that is, a structure
with two pointers: to the list head and its tail.
An EMPTY list is NOT a cons; it's the symbol `nil',
which can also be written and read as `()'.
A true/proper list is one whose last tail is `nil'.
A dotted list is one whose last tail is not `nil'.
(It can be any value other than `nil'.)
Every non-empty list can be written using dot
notation: e.g., (a . (b . c)), whether or not c is
nil. That's usually abbreviated as (a b . c), or
if c is nil, as (a b c).
> If you want to work on a "local" version of a list, you can copy that
> list yourself. `append' is one way to do it;
`C-h f append' tells you:
The last argument is NOT copied, just used as the
tail of the new list.
This is important. The tail of the result is last
argument. So you can't copy a list by passing it
alone to `append':
(let ((xs (list 1 2 3))) (eq xs (append xs)))
; ==> t
But you can copy a list by appending nil to it:
(let ((xs (list 1 2 3))) (eq xs (append xs ())))
; ==> nil
> `cl-copy-list' is another.
Or `copy-sequence' or its alias `cl-copy-seq'.
___
You can think in terms of pointers, if that helps.
But Lisp doesn't have pointers in the usual sense
of the term. You can't pass explicit pointers as
arguments, etc. The "pointers" are used implicitly.
What's true is that when you pass a value, a _copy_
of the value is not created and passed. So yes, a
pointer to the value is passed, but not explicitly
- not a pointer as such. Such pointers are not
values/objects in the language.
In Lisp we think of the thing itself (which is a
Lisp value/object) as what is passed - not a copy,
and not a pointer to it. The thing _is_ its
pointer (which of course is exactly what you said
in your first sentence).
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-08-20 21:45 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-20 3:46 [Elisp][Question] How to modify a list by index while preserving value outside of scope? Rodrigo Morales
2023-08-20 4:08 ` Rodrigo Morales
2023-08-20 5:38 ` Marcin Borkowski
2023-08-20 21:45 ` [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.