mearning
[1, 2, 3][-2] == 2
[1, 2, 3, 4, 5][-4] == 2
[1, 2, 3, 4, 5][-2] == 4
Let's check the f
.
f([1, 2, 3], 1) = 1
f([1, 2, 3], -2) = 1
f([1, 2, 3, 4, 5], -4) = 1
f([1, 2, 3, 4, 5], -2) = 3
if the second argument is positive, simply return it, or
return (len(xs) + x) % len(xs)
if it is negative.
Implementation v
f(xs, x) = x (if x is positive)
f(xs, x) = `(len(xs) + x) % len(xs)` (if x is negative)
e.g.
(5 - 2) % 5 = 4 % 5 = 4
(5 - 4) % 5 = 2 % 5 = 2
(3 - 2) % 3 = 2 % 3 = 2
(3 + 1) % 3 = 5 % 3 = 2