Emacs — How to replace nth element of a list with a let-bound variable

后端 未结 3 1534
Happy的楠姐
Happy的楠姐 2021-01-03 05:38

I have not found any examples of how to replace the nth element of a list without first adding every element (one-by-one

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 06:35

    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:

    1. 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=).

    2. 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).

    3. 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.

    4. 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.

提交回复
热议问题