From the end does not work, as I expect in a position

[18]> (position 3 '(1 2 3 4 5 6 7 8) :from-end nil)
2
[19]> (position 3 '(1 2 3 4 5 6 7 8) :from-end t)
2

What to do?

+3
source share
2 answers

positionworks as documented. hyperspec says :

The returned position is an index in the sequence of the leftmost (if the end is true) or the rightmost (if there is an end - this is a lie) element that satisfies the test; otherwise nil is returned. The returned index refers to the left end of the entire sequence, regardless of the value of the beginning, end, or end.

+6
source

Try to (position 3 '(1 2 3 4 3 2 1) :from-end t)see what the keyword actually does :from-end.

what you think is much simpler - (- (length list) (position elem list))

+2
source

Source: https://habr.com/ru/post/1777152/


All Articles