RepCad Posted June 12, 2021 Posted June 12, 2021 (edited) Hi all, I need a function to extract items from a list till an element, kind of like reversed of member function : List A ("aa" "bb" "cc" "dd" "ee") Target element > "cc" Result >> ("aa" "bb" "cc") Target element > "dd" Result >> ("aa" "bb" "cc" "dd" ) Target element > "aa" Result >> ("aa") Edited June 12, 2021 by amir0914 Quote
marko_ribar Posted June 12, 2021 Posted June 12, 2021 (defun foo ( el lst ) (member el (reverse lst)) ) (foo "cc" '("aa" "bb" "cc" "dd")) => ("cc" "bb" "aa") HTH. M.R. 2 Quote
Lee Mac Posted June 13, 2021 Posted June 13, 2021 (edited) Another couple - (defun foo2 ( x l ) (cond ((not l) l) ((= x (car l)) (list x)) ((cons (car l) (foo2 x (cdr l))))) ) (defun foo3 ( x l / f ) (setq f (lambda ( y ) (if (= x y) (not (setq f (lambda ( y ) t)))))) (vl-remove-if '(lambda ( z ) (f z)) l) ) Edited June 13, 2021 by Lee Mac 1 1 Quote
RepCad Posted June 14, 2021 Author Posted June 14, 2021 17 hours ago, Lee Mac said: Another couple - (defun foo2 ( x l ) (cond ((not l) l) ((= x (car l)) (list x)) ((cons (car l) (foo2 x (cdr l))))) ) (defun foo3 ( x l / f ) (setq f (lambda ( y ) (if (= x y) (not (setq f (lambda ( y ) t)))))) (vl-remove-if '(lambda ( z ) (f z)) l) ) Thank you lee.mac marko_ribar, your function needs an extra Reverse. Quote
mhupp Posted June 14, 2021 Posted June 14, 2021 This is what i did for picking how many tabs of a template i wanted to insert. (setq lst '(Cover D01 D02 D03 D04 D05 D06 D07 D08 D09 D10)) (if (not (setq i (getreal "Number of Drawers [1-10]<10>:"))) (setq i 10) ) (setq i (1+ i)) (while (< i (vl-list-length lst)) (setq lst (vl-remove (nth i lst) lst)) ) 1 Quote
Tharwat Posted June 14, 2021 Posted June 14, 2021 Another with While function. (defun Up:to:within ( itm lst / out ) (if (vl-position itm lst) (progn (while (and (/= (setq rtn (car lst)) itm) (setq out (cons rtn out)) (setq lst (cdr lst)))) (setq out (reverse (cons itm out))) ) ) ) Usage: (Up:to:within "dd" '("aa" "bb" "cc" "dd" "ee")) 2 Quote
Jonathan Handojo Posted June 21, 2021 Posted June 21, 2021 Out of curiosity, what should (foo "bb" '("aa" "bb" "cc" "aa" "bb" "cc" "dd")) return? Quote
Steven P Posted June 21, 2021 Posted June 21, 2021 Reading the question I would say "aa" "bb" which is what Lee Mac and Tharwat do Quote
ronjonp Posted June 21, 2021 Posted June 21, 2021 Another: (defun _foo (i l / n r) (if (setq n (vl-position i l)) (progn (repeat (1+ n) (setq r (cons (car l) r)) (setq l (cdr l))) (reverse r)) l ) ) (_foo "cc" '("aa" "bb" "cc" "dd" "ee")) 1 Quote
Lee Mac Posted June 21, 2021 Posted June 21, 2021 Another: (defun foo4 ( x l / r ) (vl-some '(lambda ( y ) (setq r (cons y r)) (= x y)) l) (reverse r) ) Quote
ronjonp Posted June 21, 2021 Posted June 21, 2021 51 minutes ago, Lee Mac said: Another: (defun foo4 ( x l / r ) (vl-some '(lambda ( y ) (setq r (cons y r)) (= x y)) l) (reverse r) ) Nice! Quote
pBe Posted June 26, 2021 Posted June 26, 2021 ;; while (Defun foo6 (ta l / j ls) (while (and (setq j (Car l)) (/= j ta)) (setq ls (cons j ls) l (Cdr l)) ) (reverse (Cons j ls)) ) ;; repeat (Defun foo7 (ta l / ls) (repeat (- (length l) (vl-position ta l)) (setq j (Car l) ls (cons j ls ) l (Cdr l)) ) (reverse ls) ) 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.