Arepo Posted February 13, 2013 Posted February 13, 2013 I need some code to remove 2 specific consecutive items from a list. Ex: From list '(a b c d e f g h) I need to remove a & b and get '(c d e f g h) but ONLY if b is right after a. If a or b are separate elements in the list they shouldn't be removed. Any help would be appreciated. Thank you. Quote
BIGAL Posted February 14, 2013 Posted February 14, 2013 Use "nth" step through list find "a" If then get next nth if b then delete from list (setq mylst (list "x" "a" "b" "C" "d")) (setq numb (length mylst)) (setq x 0) (repeat numb (princ (nth x mylst)) (setq x (+ x 1)) ) Quote
pBe Posted February 14, 2013 Posted February 14, 2013 (edited) Wasnt counting on duplicate elements so EDIT: if the intention was "rest of the list after pair" (Defun[color="blue"] [b]rem2rest [/b]([/color]e1 e2 lst) (mapcar '(lambda (x y) (if (and (eq x e1)(eq y e2)) (setq lst (cdr (member y[b][b] (member x lst)[/b][/b]))) ) ) lst (cdr lst)) lst ) Edited February 14, 2013 by pBe busted by Lee Mac :) Quote
Lee Mac Posted February 14, 2013 Posted February 14, 2013 Careful pbe _$ (rem2 'a 'b '(c b d e a b c)) (D E A B C) If I've understood the OP, perhaps: (defun rem2 ( e1 e2 lst ) (apply 'append (mapcar (function (lambda ( a b c ) (if (not (or (and (equal a e1) (equal b e2)) (and (equal b e1) (equal c e2)) ) ) (list b) ) ) ) (cons nil lst) lst (append (cdr lst) '(())) ) ) ) _$ (rem2 'a 'b '(c b d e a b c)) (C B D E C) Quote
pBe Posted February 14, 2013 Posted February 14, 2013 (edited) Careful pbe _$ (rem2 'a 'b '(c b d e a b c)) (D E A B C) Ow shoot you're right " Remove a Pair of Specific Items from List" .. me thought its rest of the list after pair Quick fix (Defun rem2pBe (e1 e2 lst / l lst l2) [color="blue"](while (and (setq x (car lst))(setq b (cdr lst))(null l2)) (setq y (car b)) (if (and (eq x e1)(eq y e2)) (setq l2 (append (reverse l) (cdr (member y (member x lst)))) l3 l2) (setq l (cons x l))) (setq lst b) )[/color] [color="red"] [b] (if (> (length lst) 1) (rem2pBe e1 e2 l2) l3)[/b][/color] ) EDIT: Recursive version Edited February 15, 2013 by pBe busted again :D Quote
Lee Mac Posted February 14, 2013 Posted February 14, 2013 Careful pBe... _$ (rem2 'a 'b '(c d e a b)) (C D) _$ (rem2 'a 'b '(a b c d)) (B C D) _$ (rem2 'a 'b '(c a b d e a b c)) (C B D E C) sorry Quote
pBe Posted February 14, 2013 Posted February 14, 2013 (edited) Careful pBe.......sorry No worries Lee, Now i'm a firm believer of "haste make waste" . I'll focus and re-post the code EDIT: Almost there. now all i need to figure out is duplicate pairs .... EDIT: Code update (recursive) Edited February 14, 2013 by pBe Quote
Stefan BMR Posted February 14, 2013 Posted February 14, 2013 (defun rem2 (a b l) (if (cdr l) (if (and (eq (car l) a) (eq (cadr l) b)) (rem2 a b (cddr l)) (cons (car l) (rem2 a b (cdr l))) ) l ) ) Quote
pBe Posted February 14, 2013 Posted February 14, 2013 (defun rem2 (a b l) (if (cdr l) (if (and (eq (car l) a) (eq (cadr l) b)) ([b]rem2 [/b]a b (cddr l)) (cons (car l) ([b]rem2[/b] a b (cdr l))) ) l ) ) I've seen it done before, but i personally have not tried that construct . ... Very good Stefan X2 Quote
Arepo Posted February 14, 2013 Author Posted February 14, 2013 (edited) Many thanks everybody for the prompt help! Edited February 14, 2013 by Arepo Quote
Arepo Posted February 14, 2013 Author Posted February 14, 2013 Stefan, codul e grozav de ingenios! I used equal instead eq since my list elements are dotted pairs and the code doesn't work with eq. Unlike pBe, I haven't even seen this before. Quote
BIGAL Posted February 14, 2013 Posted February 14, 2013 Pbe lee-mac.com "remove item from list" Quote
pBe Posted February 15, 2013 Posted February 15, 2013 Pbe lee-mac.com "remove item from list" it appears to that way Bigal Quote
Stefan BMR Posted February 15, 2013 Posted February 15, 2013 Stefan, codul e grozav de ingenios! I used equal instead eq since my list elements are dotted pairs and the code doesn't work with eq. Unlike pBe, I haven't even seen this before. You're welcome Arepo. I'm glad you like it I've seen it done before, but i personally have not tried that construct . ...X2 Thank you my friend I've seen it and I've used it (most probably in this order...) before. Some other sample here and here. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.