Timeline for What does setcdr do to a list which is the return value of a function?
Current License: CC BY-SA 4.0
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jan 4, 2022 at 13:34 | vote | accept | Arch Stanton | ||
| Jan 2, 2022 at 22:58 | comment | added | Arch Stanton | I clear the FH right after that so I don't care | |
| Jan 2, 2022 at 22:57 | comment | added | db48x | Yes, append copies FH, but nreverse destructively modified FH first. Not sure if that’s what you want. | |
| Jan 2, 2022 at 22:55 | comment | added | Arch Stanton | Have a good night there's no hurry :-) > it sounds like you need to insert a copy of the forward history into the backwards history… I've got (setcdr <BH> (append (nreverse <FH>) (cdr <BH>))). The forward history isn't the last argument in the append form, so it should be copied, right? Now that I think about it it looks like to the last example in the manual page about setcdr, only with append instead of cons because I'm inserting a list. | |
| Jan 2, 2022 at 22:42 | comment | added | Arch Stanton | > binding a list to a variable doesn’t copy the list It wasn't so much the binding that I thought could copy the list, but rather the returning. I thought a list as a return value of a function could be a different object than the list as referenced by a variable. Actually you helped me formalize this thought better. | |
| Jan 2, 2022 at 22:40 | comment | added | db48x | I’m a bit sleep–deprived at the moment, so I’m not sure I understand what you’re doing exactly. However, it sounds like you need to insert a copy of the forward history into the backwards history, so that they aren’t linked together. | |
| Jan 2, 2022 at 22:09 | comment | added | Arch Stanton | > If you’re making a list that holds history… Sorry I imagined that you would advise against what I wrote, I was trying not to get into the details of my program but maybe I better explain. I have two lists, a backward history and a forward history (where newer positions are stored when I jump to the old ones) (identical to point-stack so far). When I push a new pos to the BH, if the FH is non-nil I use setcdr to insert the FH past the most recent pos in the BH. As a result the whole history tree is saved. | |
| Jan 2, 2022 at 21:45 | comment | added | db48x | If you’re making a list that holds history, consider adding items to the beginning of the list with cons instead of adding them to the end with setcdr. Since this creates new conses, you can control what they are shared with more easily. | |
| Jan 2, 2022 at 21:41 | comment | added | db48x | copy-sequence will work fine as well (copy-list is just easier to remember). When Emacs reads something like (setq first …), it creates a list in memory containing the symbol setq followed by first followed by whatever the value is. That list is the source code that gets passed to eval to be run. If your real program is doing something else, then you can ignore that part. The rule still applies: binding a list to a variable doesn’t copy the list; the variable simply has a reference to the list. If you modify it, you are modifying the one single copy of your list that exists. | |
| Jan 2, 2022 at 21:28 | comment | added | Arch Stanton | Thanks! Please bear with me, I still have some doubts. 1. This fact that the tail of the list is still shared with my source code... What does it mean practically? 2. My actual list isn't defined in the source code, it's built by pushing markers onto it and it works as a history. The setcdr is in the function that updates the history. I (believe I) need to update it in place, so should I really copy the list? 3. I didn't know about cl-copy-list but I had experimented with copy-sequence, is it okay too? | |
| Jan 2, 2022 at 20:39 | history | answered | db48x | CC BY-SA 4.0 |