swap elements in list

swap elements in list

Anonymous
Not applicable
1,973 Views
5 Replies
Message 1 of 6

swap elements in list

Anonymous
Not applicable

I have a list  that is like '(a b  d c e f g h) I am trying to swap the element  only  d an dc. I am using subst to swap that value in mapcar 'lambda but unable to get output.

0 Likes
1,974 Views
5 Replies
Replies (5)
Message 2 of 6

CodeDing
Mentor
Mentor

@Anonymous ,

 

I'm interested to see what other methods are presented here, but here's the most effective way I can think of..

It does Not use NTH or SUBST or MEMBER, in case a list has duplicates.. using the Nth position is the most accurate way I can think of to ensure no error occurs.

(defun SwapNeighbors (n lst / tmp)
  ;n - int, of first item to swap w/ item immediately after.
  ;lst - list, of items
  ;Ex: (SwapNeighbors 2 '(a b c d e f)) <-- will return --> '(a b d c e f)
  (if (< -1 n (1- (length lst)))
    (progn
      (repeat n
        (setq tmp (cons (car lst) tmp))
        (setq lst (cdr lst))
      );repeat
      (append (reverse tmp)
              (list (cadr lst) (car lst))
              (cddr lst)
      );append
    );progn
  ;else
    lst
  );if
);defun

Best,

~DD

0 Likes
Message 3 of 6

john.uhden
Mentor
Mentor

I dug through some old stuff and put together the following...

;;**********************************************
;; NOTE: To cut all, use (vl-remove |item |list)
;;----------------------------------------------
;; Tony Tanzillo (01-25-02)
(defun @Anonymous_nth (lst pos / head)
   (repeat pos
       (setq head (cons (car lst) head)
             lst (cdr lst)
       )
   )
   (append (reverse head) (cdr lst))
)
;;-----------------------------------------------------------------------
;; John's "New and Improved" nthadd (09-18-01) using Herman's approach of
;; repeat instead of horrendously slow append:
;; data = list being inflated
;; item = item (or list of items) to add to data
;; n    = 'INT position in data list to add item (zero counter)
;; multiple = 1  = item is a list; add each element
;;          = nil = add item as is
;; incorporated with multiple option (09-28-01)
;; Kosterized (02-02-03)
;; (02-04-03) corrected for VEDIT
(defun @Anonymous_nth (data item n multiple / len head)
   (and
      (listp data)
      (setq len (vl-list-length data))
      (= (type n) 'INT)
      (>= n 0)
      (setq n (min n len)) ; corrected from (<= n len)
      (progn
        (repeat n
           (setq head (cons (car data) head)
                 data (cdr data)
           )
        )
        (if (and multiple (listp item)(vl-list-length item))
           (foreach element (reverse item)(setq data (cons element data)))
           (setq data (cons item data))
        )
        (repeat n
           (setq data (cons (car head) data)
                 head (cdr head)
           )
        )
      )
   )
   data
)
(defun @swap (item1 item2 old / n1 n2 new)
  (and
    (setq n1 (vl-position item1 old))
    (setq n2 (vl-position item2 old))
    (setq new (@cut_nth old n1)
          new (@add_nth new item2 n1 nil)
          new (@cut_nth new n2)
          new (@add_nth new item1 n2 nil)
    )
  )
  (if new new old)
)

John F. Uhden

0 Likes
Message 4 of 6

ВeekeeCZ
Consultant
Consultant
(defun Swap34 (l) (mapcar '(lambda (x) (nth x l)) '(0 1 3 2 4 5)))
Message 5 of 6

Sea-Haven
Mentor
Mentor

Found this you would run twice save 1st val & 2nd val then run other order 2nd then 1st.

 

By Bill Kramer.

(defun subst_it (n new data_list)
(cond
((minusp n) data_list)
((zerop n) (cons new (cdr data_list)))
(T (cons (car data_list) (subst_it (1- n) new (cdr data_list))))
)
)

where:
N is the position of the element to be replaced,
NEW is the new value, and obviously
DATA_LIST is the list to be modified.
0 Likes
Message 6 of 6

ronjonp
Mentor
Mentor

Here are a couple more that may help:

 

(defun _swapnth	(l nth1 nth2 / a n)
  ;; RJP » 2020-08-10
  ;; Zero based index!
  (setq n -1)
  (setq a (list (cons nth2 (nth nth1 l)) (cons nth1 (nth nth2 l))))
  (mapcar '(lambda (x)
	     (cond ((cdr (assoc (setq n (1+ n)) a)))
		   (x)
	     )
	   )
	  l
  )
)
;; (_swapnth '(1 2 3 4 5 6 7 8 9 10) 0 9)
;; (10 2 3 4 5 6 7 8 9 1) 

(defun _replacewith (l 2swp 2rep)
  ;; RJP » 2020-08-10
  (mapcar '(lambda (x)
	     (cond ((equal x 2swp) 2rep)
		   (x)
	     )
	   )
	  l
  )
)
;; (_replacewith '(5 2 3 4 5 6 7 8 9 10 5) 5 "»")
;; ("»" 2 3 4 "»" 6 7 8 9 10 "»") 

 

0 Likes