Jump to content

Recommended Posts

Posted

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.

Posted

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))
)

Posted (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 by pBe
busted by Lee Mac :)
Posted

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)

Posted (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 :lol:

 

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 by pBe
busted again :D
Posted

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

Posted (edited)
Careful pBe...

....sorry

 

:lol: 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 by pBe
Posted
(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
   )
 )

Posted
(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
   )
 )

 

:thumbsup: I've seen it done before, but i personally have not tried that construct . :book: ...

 

Very good Stefan :thumbsup:

 

X2

Posted (edited)

Many thanks everybody for the prompt help!

Edited by Arepo
Posted

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.

Posted
Pbe lee-mac.com "remove item from list"

 

:lol: it appears to that way Bigal

Posted
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

 

:thumbsup: I've seen it done before, but i personally have not tried that construct . :book: ...

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.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...