I have not found any examples of how to replace the nth
element of a list without first adding every element (one-by-one
Adding a late answer that's specifically about add-to-ordered-list
, because I think there's confusion about what it does and how it works.
add-to-ordered-list
lets you specify (record) a specific order for an individual list element.
But the list can also have elements that have no specific order recorded for them. These elements are always put at the end of the list, after all of the elements that do have specific recorded orders.
If you want to replace an element that has a recorded order then you need to remove it from the list and add its replacement (which is what you guessed).
(add-to-ordered-list 'the-list 'a 1) ; Records order 1 for element (symbol) a.
(delq a the-list) ; Removes a from the list.
(add-to-ordered-list 'the-list 'HELLO 1) ; Records order 1 for element (symbol) HELLO.
Some things to add, to help clarify a bit:
You can't use a string element, such as "HELLO"
, because recording orders for elements requires them to be distinguishable using eq
(not equal
or string=
).
You can remove the recorded order from an element, instead of removing the element from the list. If you do that, the element remains in the list, but moves after any elements that have recorded orders. That's not what you wanted here (you wanted to replace it), but know that it's possible. You do this by calling add-to-ordered-list
with the element and with no ORDERED
value (or a nil
value).
Orders recorded for elements need not be specified for all elements (see above), and they need not be consecutive. All that matters is that they can be compared numerically, so their elements can be ordered. For example, you can add elements a
, b
, and c' to an empty list, recording orders of 13, 42, and 6 for them, respectively. The result is the list
(c a b)`, because 6 < 12 < 42.
You can even record the same order for more than one list element, in which case those elements are consecutive in the list but the order among them is unimportant. You can think of the recorded orders as buckets or scores. All elements with the score 3 appear in the list before all elements with the score 7, and elements with score 3 (or 7) are consecutive.
See also this emacs.SE question.