I am interested in the swap-arg operator, which takes as input signal 1) a function f of n variables and 2) an index k, and then returns the same function, except for the first and kth input, the variables are swapped. for example (in mathematical notation):
(swap-arg (f, 2)) (x, y, z, w) = f (z, y, x, w)
Now my first idea is to implement this with rotatef as follows:
(defun swap-args (fk)
(lambda (L) (f (rotatef (nth k L) (car L)))))
However, this seems inelegant since it uses rotatef on input. Furthermore, it is O (n) and may be O (n ^ 2) in practice if applied repeatedly to reindex everything.
This seems like a common problem that people have already thought about, but I could not find anything. What a good way to replace data like this? Is there a standard method that people use?
source
share