Jump to content

Recommended Posts

Posted (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 by amir0914
  • RepCad changed the title to Extract items from a list
Posted
(defun foo ( el lst )
  (member el (reverse lst))
)

 

(foo "cc" '("aa" "bb" "cc" "dd")) => ("cc" "bb" "aa")

 

HTH. M.R.

  • Like 2
Posted (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 by Lee Mac
  • Like 1
  • Thanks 1
Posted
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.

Posted

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

 

  • Like 1
Posted

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

 

  • Like 2
Posted

Out of curiosity, what should (foo "bb"  '("aa" "bb" "cc" "aa" "bb" "cc" "dd")) return?

Posted

Reading the question I would say "aa" "bb" which is what Lee Mac and Tharwat do

Posted

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

 

  • Like 1
Posted

Another:

(defun foo4 ( x l / r )
    (vl-some '(lambda ( y ) (setq r (cons y r)) (= x y)) l)
    (reverse r)
)

 

Posted
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!

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

 

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