TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: subbup on June 29, 2004, 07:07:19 AM
-
I know the position of an item in a list.
Till that position I want to remove all the items.
Is there any function to do this.
-
have you looked into vl-remove ?
-
It's removing all the duplicates in a list.
but I want to delete upto particular position in a list.
-
this is as beautiful as some but.......
(setq lst (list 23 23 43 54 90 77 88 99))
(23 23 43 54 90 77 88 99)
; remove the first three elements
(repeat 2 (setq lst (vl-remove (nth 0 lst) lst)))
(54 90 77 88 99)
-
Using the same list as Mark:
(defun list_trim (lst posit / )
(repeat posit (setq lst (cdr lst)))
lst
)
_$ (setq lst (list 23 23 43 54 90 77 88 99))
(23 23 43 54 90 77 88 99)
_$ (list_trim lst 3)
(54 90 77 88 99)
_$ (list_trim lst 5)
(77 88 99)
_$ (list_trim lst 2)
(43 54 90 77 88 99)
_$ (list_trim lst 1)
(23 43 54 90 77 88 99)
BTW Mark, can you see why your code removes the first three elements of your list when you only run repeat twice?
-
Hay that's fun. :)
And useful too.
(defun list_Rtrim (lst posit / )
(setq lst (reverse lst))
(repeat posit (setq lst (cdr lst)))
(reverse lst )
)
(defun list_Ltrim (lst posit / )
(repeat posit (setq lst (cdr lst)))
)
(defun list_trim (lst left right / )
(list_Ltrim (list_Rtrim lst right) left)
)
(setq lst (list 0 1 2 3 4 5 6 7 8 9))
_$ (list_ltrim lst 4)
(4 5 6 7 8 9)
_$ (list_rtrim lst 4)
(0 1 2 3 4 5)
_$ (list_trim lst 2 3)
(2 3 4 5 6)
-
Heh
.. and the recursive version?
-
You know that makes my head hurt. :oops:
-
Something like this?
(defun list_Ltrim (lst posit / )
(if (> posit 1)
(list_Ltrim (cdr lst) (1- posit))
(cdr lst)
)
)
-
Almost. What happens with (list_Ltrim alist 0)?
Or even (list_Ltrim alist -1)? I know that shouldn't be an issue but at least it shouldn't return (cdr alist)
I'd do it something like this:
(defun list_Ltrim (lst posit)
(cond ((null lst) nil)
((> posit 0) (list_Ltrim (cdr lst) (1- posit)))
(lst)
)
)
-
Ah yes, the old fox does it again.
I like it, just wish i thought of it. :)
-
*Psst* Look over here. (http://theswamp.org/phpBB2/viewtopic.php?t=635)
-
I think what's been posted, particularly:
(defun list_Ltrim (lst posit)
(cond ((null lst) nil)
((> posit 0) (list_Ltrim (cdr lst) (1- posit)))
(lst)
)
)
are very good solutions, and generally follow the approach I'd take.
But solely for fun, obfuscation and a demonstration of "thinking outside the box" consider why this works (code distilled to re-useable functions):
(defun MakeKeys ( lst / key )
(setq key -1)
(mapcar '(lambda (item) (setq key (1+ key))) lst)
)
(defun IndexList ( lst )
(mapcar 'cons
(MakeKeys lst)
lst
)
)
(defun LeftTrimList ( lst n / index )
(if (< n 1) lst
(mapcar 'cdr
(member
(assoc n (setq index (IndexList lst)))
index
)
)
)
)
(setq lst '(A B C D E F G H I))
(LeftTrimList lst 5) => (F G H I)
(LeftTrimList lst -1) => (A B C D E F G H I)
(LeftTrimList lst 99) => nil
Cheers. :)
-
Interesting approach, Michael. Quite a detour but instructive
So, a righttrim function would be
(defun RightTrimList (lst n / index)
(if (< n 1) lst
(reverse (mapcar 'cdr (member
(assoc n (setq index (IndexList (reverse lst))))
index))
)
)
)
Right?
-
Interesting approach, Michael. Quite a detour but instructive
Thank you Stig, just a knurd havin' fun. :)
<Didn't test your variant but it looks just about right ro me.>