PYTHON   26

swaplist

Guest on 22nd September 2022 12:27:29 AM

  1. (defun swap (lst i j)                           ; return lst in which i'th and j'th elements are swapped
  2.   (append                                       ; append together three lists
  3.     (butlast lst (- (length lst) i))            ; select elements of lst up to i, starting at 0
  4.     (list (nth j lst))                          ; select j'th element, and put it into a list
  5.     (nthcdr                                     ; select elements between i and j
  6.       (1+ i)                                    ; from i+1
  7.       (butlast lst (- (length lst) j)))         ; to j-1
  8.     (list (nth i lst))                          ; select the i'th element, put into a list
  9.     (nthcdr (1+ j) lst)))                       ; select elements after j

Raw Paste


Login or Register to edit or fork this paste. It's free.