vl-remove-if stupidity

vl-remove-if stupidity

john.uhden
Mentor Mentor
2,178 Views
5 Replies
Message 1 of 6

vl-remove-if stupidity

john.uhden
Mentor
Mentor

I know this might be rare, but I'm really feeling stupid.

 

Command: (setq lst '(0.0 0.1 2.0 2.1 2.7 2.8 3.0 4.0))
(0.0 0.1 2.0 2.1 2.7 2.8 3.0 4.0)

 

Command: (vl-remove-if '(lambda (a b)(equal a b 0.1)) lst)

error: too few arguments

 

Whuh!  I have more arguments at home than I need!

John F. Uhden

0 Likes
Accepted solutions (1)
2,179 Views
5 Replies
Replies (5)
Message 2 of 6

Ranjit_Singh
Advisor
Advisor

Your lambda needs a and b. So you may need to pass lst and (cdr lst)

0 Likes
Message 3 of 6

stevor
Collaborator
Collaborator

You could look at the thread:

Remove duplicate points from a lis.

I would use use  LeeMac's  LM:UniqueFuzz

or  joselggalan's   RemoveEqualPts

functions, for speed.

S
0 Likes
Message 4 of 6

Kent1Cooper
Consultant
Consultant

What kind of return are you expecting?  If you want to remove something if it's within 0.1 of an adjacent item in the list [or perhaps within 0.1 of any item, if the list might not always be in numerical order?], which of the two do you want to remove?  The lower number?  The higher number?  Both?  The first one that occurs in the list, regardless of which is lower or higher, or the second one?

 

If you want to remove the first number in any adjacent pair that are within 0.1 of each other, this comparison of each number to the one immediately following it inexplicably removes one of them [the 2.7]:

 

(vl-remove-if '(lambda (x) (equal x (cadr (member x lst)) 0.1)) lst)

returns:
(0.0 0.1 2.0 2.1 2.8 3.0 4.0)

 

whereas [equally inexplicably, if the above doesn't work as expected] if you stretch the fuzz factor just a little over 0.1, it successfully removes all of them:

 

(vl-remove-if '(lambda (x) (equal x (cadr (member x lst)) 0.101)) lst)

returns:
(0.1 2.1 2.8 3.0 4.0)

Kent Cooper, AIA
0 Likes
Message 5 of 6

john.uhden
Mentor
Mentor
That kinda makes sense. Thank you very much.
But why get the error "Too few arguments"?

John F. Uhden

0 Likes
Message 6 of 6

Kent1Cooper
Consultant
Consultant
Accepted solution

@john.uhden wrote:
.... why get the error "Too few arguments"?

Because [I think] as @Ranjit_Singh pointed out in Post 2, the (lambda) function lists two arguments, but it's only going to get one supplied by each step of its stepping through the list one item at a time.  Help for (vl-remove-if) says the predicate function [in this case the (lambda) function] "...can be any function that accepts a single argument and returns T for any user-specified condition."  That's why I tried a version with only one argument [x] and pulling what it has to compare to from the list at a position defined relative to that one.  I was very doubtful about being able to include the list variable within the (lambda) operation as well as at the end for (vl-remove-if) to apply that (lambda) function to -- possibly [who knows?] that's part of the reason it doesn't work as expected with the 0.1 fuzz factor.  But I'm completely at a loss to explain why it does with that increased a little [nor do I recall what gave me the idea to try that].

 

EDIT:  That single-argument limitation in the description for the predicate function in (vl-remove-if) is made puzzling by the fact that it then lists, among the forms that function can take:

 

'(LAMBDA (A1 A2) ...)

 

[as well as the embedded-in-(function) equivalent], which shows two arguments, and looks a lot like your original code.  I would have to assume that aspect is a typographical error in the Help page.

Kent Cooper, AIA
0 Likes