Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

9
  • 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? Commented Jan 2, 2022 at 21:28
  • 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. Commented Jan 2, 2022 at 21:41
  • 1
    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. Commented Jan 2, 2022 at 21:45
  • > 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. Commented Jan 2, 2022 at 22:09
  • 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. Commented Jan 2, 2022 at 22:40