vl-remove-if confuse

vl-remove-if confuse

thsa2501
Enthusiast Enthusiast
702 Views
11 Replies
Message 1 of 12

vl-remove-if confuse

thsa2501
Enthusiast
Enthusiast

hi everyone

this code getting from lee mac website. which I redmarked is so confusing because this code is getting nill personally checked ((= (setq i (1+ i)) n) but the code is index 3 removing the 3rd no from the list. how it is possible?

 

n= index 3

l = (list 0 1 2 3 4)

result  = '(0 1 2 4) how?

(defun LM:RemoveNth ( n l / i )
(setq i -1)
(vl-remove-if '(lambda ( x ) (= (setq i (1+ i)) n)) l)
)

 

thanks advance to everyone for helping

 

0 Likes
Accepted solutions (1)
703 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant

In the (nth) function for lists, and also in the (ssname) function for selection sets, the first item is index number 0.

Kent Cooper, AIA
0 Likes
Message 3 of 12

john.uhden
Mentor
Mentor

@thsa2501 

Though I was not at all fond of him, his contributions were outstanding...

  ;; Tony Tanzillo (01-25-02)
  (defun cut_nth (lst pos / head)
     (repeat pos
         (setq head (cons (car lst) head)
               lst (cdr lst)
         )
     )
     (append (reverse head) (cdr lst))
  )
;; where pos is 0 based just like nth

John F. Uhden

Message 4 of 12

ВeekeeCZ
Consultant
Consultant

@thsa2501 wrote:

hi everyone

this code getting from lee mac website. which I redmarked is so confusing because this code is getting nill personally checked ((= (setq i (1+ i)) n) but the code is index 3 removing the 3rd no from the list. how it is possible?

 

n= index 3

l = (list 0 1 2 3 4)

result  = '(0 1 2 4) how?

(defun LM:RemoveNth ( n l / i )
(setq i -1)
(vl-remove-if '(lambda ( x ) (= (setq i (1+ i)) n)) l)
)

 

thanks advance to everyone for helping

 


 

How did you check it? Do you understand how vl-remove-if works? Do you understand how mapcar works? It's basically a loop. So once i is 3 so the comparison is T, it's removed.

 

Change the apostrophe ' to function and trace the code step-by-step, you'll see.

 

(defun LM:RemoveNth ( n l / i )
  (setq i -1)
  (vl-remove-if (function (lambda ( x ) (= (setq i (1+ i)) n))) l)
  )

 

0 Likes
Message 5 of 12

martti.halminen
Collaborator
Collaborator

Another way of doing this:

 

(defun remove-nth (n lst)
  ;; removes the nth item of the list, numbering starts from zero
  (if (or (null lst)(minusp n)(> n (1-(length lst))))
      lst
      (remove-nth-int n lst)))

(defun remove-nth-int (n lst)
  ;; Internals of remove-nth
  (cond
    ((zerop n) (cdr lst))
    (T (cons (car lst)
             (remove-nth-int (1- n) (cdr lst))))))
0 Likes
Message 6 of 12

Sea-Haven
Mentor
Mentor

There is a sequence of list manipulation examples over at the swamp, look for the [Challenge] Axx posts multiple answers for this question.

 

AutoLISP (Vanilla / Visual) (theswamp.org)

Message 7 of 12

thsa2501
Enthusiast
Enthusiast

thank you so much for your reply @ВeekeeCZ 

 

i know how mapcar works, but i don't know how works vl-remove-if that is so confusing for me.

 

i have a question about (= (setq i (1+ i)) n)

already the value set is (setq i -1) .

this is my point view.it becomes nil value (= (setq i (1+ -1(i value)) 3 (n-input from user)).it becomes (= 0 3)  this is not equal.isn't it? 

(vl-remove-if nil '(0 1 2 3 4))

the final answer is '(0 1 2 3 4)).this is what i understood.

 

 

 

0 Likes
Message 8 of 12

ВeekeeCZ
Consultant
Consultant
Accepted solution

No, vl-remove-if works in the same way as mapcar.

Applies a function to each member of the list. If the function returns nil, then leaves the member, otherwise, deletes the member from the list.

 

Say you have this:

l = (list 2 4 6 8 10)
n = 4
(vl-remove-if '(lambda ( x ) (= n x)) l)

So it goes

(= 4 2)? nil               at this point x=2, n=4, so lambda is (= 4 2)

(= 4 4)? True           at this point x=4, n=4, so lambda is (= 4 4)

(= 4 6)? nil

(= 4 8)? nil

(= 4 10)? nil

The result is '(2 6 8 10)

 

Now your func.

l = (list 2 4 6 8 10)
n = 4
i = -1
(vl-remove-if '(lambda ( x ) (= (setq i (1+ i)) n)) l)

(= 0 4)? nil         (note: x=2 !!)

(= 1 4)? nil         (note: x=4 !!)

(= 2 4)?nil         (note: x=6 !!)

(= 3 4)? nil         (note: x=8 !!)

(= 4 4)? True         (note: x=10 !!)

The result is '(2 4 6 8 )

Message 9 of 12

thsa2501
Enthusiast
Enthusiast

thank you so much for your reply.

it is so nice example, but I want to know how its works "vl-remove-if" function with lambda.

0 Likes
Message 10 of 12

ВeekeeCZ
Consultant
Consultant

I've added bold expressions.

I would prefer you to think about it a bit and not reply based on the first impression in a matter of secs.

 

0 Likes
Message 11 of 12

thsa2501
Enthusiast
Enthusiast

hi @ВeekeeCZ 

i almost understood. the last question don't be mind.

 

'(lambda ( x ) (= (setq i (1+ i)) n)) where is "x" inside the (setq i (1+ i)) becauce defined lambda (x)

(vl-remove-if '(lambda ( x ) (= n x)) l) i completely understood your function.


 

 

 

0 Likes
Message 12 of 12

thsa2501
Enthusiast
Enthusiast

kindly ignore last message @ВeekeeCZ 

0 Likes