I made my implement of vl-remove-if,and some much weird things happened!

I made my implement of vl-remove-if,and some much weird things happened!

ivanovsky
Enthusiast Enthusiast
711 Views
4 Replies
Message 1 of 5

I made my implement of vl-remove-if,and some much weird things happened!

ivanovsky
Enthusiast
Enthusiast

Some guy's cad system has no vl-* functions,and I made some for him.most of implements I made are work well,but some of them are not.

Here is my implement.

 

my vl-remove-if,the func 1, works well.

 

(defun xg-remove-if  (fn lst)
  (cond ((null lst) nil)
        (t
         (if (apply fn (list (car lst)))
           (xg-remove-if fn (cdr lst))
           (cons (car lst) (xg-remove-if fn (cdr lst)))))))

 

and this is my vl-remove,the fun 2,works well too.

 

(defun xg-remove (elem lst) (xg-remove-if '(lambda (x) (equal elem x)) lst))

 

 and then,I made my vl-remove-if-not,the fun 3, I would like it to work too,but it doesn't work!

 

(defun xg-remove-if-not  (fn lst)
  (xg-remove-if '(lambda (x) (not (apply fn (list x)))) lst))

 

I run it,the visual ide run out of it's memory and brokedown!

Why? I'm totally confused,I have no idea what happened.

 

Therefore, I've tried another way, I made my 2nd version of vl-remove-if-not,the fun 4, and it works!

 

(defun xg-remove-if-not2  (fn lst)
  (vl-remove-if '(lambda (x) (not (apply fn (list x)))) lst))

 

 

Anyway, I have to deliver my functions to my guy,I made my 3rd version of vl-remove-if-not, the ugly one, the fun 5, not elegant at all.Whatever,it works well.

 

(defun xg-remove-if-not  (fn lst)
  (cond ((null lst) nil)
        (t
         (if (not (apply fn (list (car lst))))
           (xg-remove-if-not fn (cdr lst))
           (cons (car lst) (xg-remove-if-not fn (cdr lst)))))))

 

 

I wonder to know whether there is kind of bug in visual ide, because I don't find out bugs in my functions.

I rewrote those functions in dr racket, and all of them work well.

Is there anyone who could help me know what happened? Thank you for reading. 

0 Likes
Accepted solutions (1)
712 Views
4 Replies
  • Lisp
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

Here's the fix.

 

(defun xg-remove-if-not  (fn1 lst)
  (xg-remove-if '(lambda (x) (not (apply fn1 (list x)))) lst))

 

Message 3 of 5

ivanovsky
Enthusiast
Enthusiast

OMG! AMAZING! AWSOME!

Thank you very much, Mr/Mz Z9E3zK5E!

Is this some kind of black magic?

Why must I change the name of 1st argument?

It confused me so much!

0 Likes
Message 4 of 5

ВeekeeCZ
Consultant
Consultant

See THIS screencast from debugging this:

 

(defun xg-remove-if  (fn lst)
  (print fn)  ; should be lambda
  (print lst) ; should be '(1 0 1 0)
  
  (cond ((null lst) nil)
        (t
         (if (apply fn (list (car lst)))
           (xg-remove-if fn (cdr lst))
           (cons (car lst) (xg-remove-if
			     (progn (print fn) fn) ; should be lambda
			     (cdr lst)))))))


(defun xg-remove-if-not  (fn lst)
  (print fn)  ; should be zerop
  (print lst) ; should be '(1 0 1 0)
  (xg-remove-if (function (lambda (x) (not (apply (progn (print fn) fn) ; should be zerop
						  (list x))))) lst))


(defun c:Test ()
  (xg-remove-if-not 'zerop '(1 0 1 0)))

 

Simply the fn arguments are in conflict. 

fn from xg-remove-if-not (= zerop) is a global to the xg-remove-if func

but it's overwritten by xg-remove-if's argument fn (= lambda) argument.

These names have to differ.

 

Another question is whether lst arg should differ too. I would rather rename it too even it does no harm in this case since the overwriting value is the same as the original.

HTH

 

0 Likes
Message 5 of 5

ivanovsky
Enthusiast
Enthusiast

Thank you very much, your anwsers are very concrete, I think I've understood what you said.Thanks again.

0 Likes