Kowal Posted January 24, 2017 Share Posted January 24, 2017 How to remove items contained on an even position in the list. Example: '(1 2 3 4 5 6 7 8 9) Result: '(2 4 6 Quote Link to comment Share on other sites More sharing options...
marko_ribar Posted January 24, 2017 Share Posted January 24, 2017 (defun odd (lst) (if lst (cons (car lst) (odd (cddr lst)))) ) (defun even (lst) (if lst (cons (cadr lst) (even (cddr lst)))) ) Quote Link to comment Share on other sites More sharing options...
Roy_043 Posted January 24, 2017 Share Posted January 24, 2017 FTFY: (defun even (lst) (if (cdr lst) (cons (cadr lst) (even (cddr lst)))) ) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted January 24, 2017 Share Posted January 24, 2017 Iterative: (defun remove-odd ( l ) ((lambda ( i ) (vl-remove-if '(lambda ( x ) (= 1 (rem (setq i (1+ i)) 2))) l)) -1) ) (defun remove-even ( l ) ((lambda ( i ) (vl-remove-if '(lambda ( x ) (= 0 (rem (setq i (1+ i)) 2))) l)) -1) ) Quote Link to comment Share on other sites More sharing options...
satishrajdev Posted January 24, 2017 Share Posted January 24, 2017 (edited) This works with both Integers & Real numbers including duplicate elements :- (defun even (l) (vl-remove-if-not '(lambda (x) (wcmatch (vl-princ-to-string (/ x 2.)) "*.0")) l ) ) (defun odd (l) (vl-remove-if '(lambda (x) (wcmatch (vl-princ-to-string (/ x 2.)) "*.0")) l ) ) Examples : _$ (EVEN '(1.0 2.0 3.0 4.0 5.0 6.0 5.0 4.0 3.0 2.0 1.0)) (2.0 4.0 6.0 4.0 2.0) _$ (ODD '(1.0 2.0 3.0 4.0 5.0 6.0 5.0 4.0 3.0 2.0 1.0)) (1.0 3.0 5.0 5.0 3.0 1.0) _$ (ODD '(1 2 3 4 5 6 5 4 3 2 1)) (1 3 5 5 3 1) _$ (EVEN '(1 2 3 4 5 6 5 4 3 2 1)) (2 4 6 4 2) Edited January 25, 2017 by satishrajdev Quote Link to comment Share on other sites More sharing options...
David Bethel Posted January 24, 2017 Share Posted January 24, 2017 (edited) If you actually want to remove the atoms from the list: [b][color=BLACK]([/color][/b]defun c:removep [b][color=FUCHSIA]([/color][/b]/ lst a i rl[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq lst '[b][color=NAVY]([/color][/b]1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]initget 7 [color=#2f4f4f]"Even Odd"[/color][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq a [b][color=NAVY]([/color][/b]getint [color=#2f4f4f]"\nRemove Filter Type [b][color=MAROON]([/color][/b] Even/Odd [b][color=MAROON])[/color][/b] Or Atom Placement: "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq i 0[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]foreach v lst [b][color=NAVY]([/color][/b]cond [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]eq a [color=#2f4f4f]"Odd"[/color][b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]and [b][color=BLUE]([/color][/b]zerop [b][color=RED]([/color][/b]rem [b][color=PURPLE]([/color][/b]1+ i[b][color=PURPLE])[/color][/b] 2[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]setq rl [b][color=RED]([/color][/b]cons v rl[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] [b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]eq a [color=#2f4f4f]"Even"[/color][b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]and [b][color=BLUE]([/color][/b]zerop [b][color=RED]([/color][/b]rem i 2[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]setq rl [b][color=RED]([/color][/b]cons v rl[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] [b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]numberp a[b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]and [b][color=BLUE]([/color][/b]not [b][color=RED]([/color][/b]zerop [b][color=PURPLE]([/color][/b]rem [b][color=TEAL]([/color][/b]1+ i[b][color=TEAL])[/color][/b] a[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]setq rl [b][color=RED]([/color][/b]cons v rl[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]setq i [b][color=MAROON]([/color][/b]1+ i[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq rl [b][color=NAVY]([/color][/b]reverse rl[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]prin1 rl[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] This could also work with integer values. Say remove every 3rd atom -David PS Extracting would do just the opposite. Edited January 24, 2017 by David Bethel extract vs remove Quote Link to comment Share on other sites More sharing options...
hanhphuc Posted January 24, 2017 Share Posted January 24, 2017 (defun even (l) (vl-remove-if ''((x)(zerop (logand (vl-position x l)1))) l)) (defun odd (l) (vl-remove-if-not ''((x)(zerop (logand (vl-position x l)1))) l)) Quote Link to comment Share on other sites More sharing options...
Roy_043 Posted January 24, 2017 Share Posted January 24, 2017 (defun ListRemoveAlt (lst includeFirstP) (vl-remove-if '(lambda (x) (setq includeFirstP (not includeFirstP))) lst ) ) Quote Link to comment Share on other sites More sharing options...
Tharwat Posted January 24, 2017 Share Posted January 24, 2017 A bit late to party. (defun even (lst / l) (mapcar '(lambda (n) (and (zerop (rem n 2)) (setq l (append l (list n))))) lst) l ) (defun odd (lst / l) (mapcar '(lambda (n) (and (not (zerop (rem n 2))) (setq l (append l (list n))))) lst) l ) Quote Link to comment Share on other sites More sharing options...
BIGAL Posted January 25, 2017 Share Posted January 25, 2017 Do we have TEN ways !!! Oh yeah (nth (+ x 1) lst) Some may read this post mistaking it for a even and odd routines for a list but. (setq lst (list 1 2 3 3 4 5 6 6 7 8 8 8 )) (1 3 4 6 7 Quote Link to comment Share on other sites More sharing options...
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.