Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are `not-equal?` and similar negated comparisons not built into Racket?

Tags:

scheme

racket

In Racket (and other Schemes, from what I can tell), the only way I know of to check whether two things are not equal is to explicitly apply not to the test:

(not (= num1 num2)) 
(not (equal? string1 string2))

It's obviously (not (that-big-of-deal?)), but it's such a common construction that I feel like I must be overlooking a reason why it's not built in.

One possible reason, I suppose, is that you can frequently get rid of the not by using unless instead of when, or by switching the order of the true/false branches in an if statement. But sometimes that just doesn't mimic the reasoning that you're trying to convey.

Also, I know the negated functions are easy to define, but so is <=, for example, and that is built in.

What are the design decisions for not having things like not-equal?, not-eqv?, not-eq? and != in the standard library?

like image 587
ohspite Avatar asked Aug 24 '16 04:08

ohspite


2 Answers

First, you are correct that it is (not (that-big-of-a-deal?))1

The reason Racket doesn't include it out of the box is likely just because it adds a lot of extra primitives without much benefit. I will admit that a lot of languages do have != for not equal, but even in Java, if you want to do a deep equality check using equals() (analogous to equal? in Racket), you have to manually invert the result with a ! yourself.

Having both <= and > (as well as >= and <) was almost certainly just convenient enough to cause the original designers of the language to include it.

So no, there isn't any deep reason why there is not any shortcut for having a not-eq? function built into Racket. It just adds more primitives and doesn't happen to add much benefit. Especially as you still need not to exist on its own anyway.

1I love that pun by the way. Have some imaginary internet points.

like image 91
Leif Andersen Avatar answered Oct 17 '22 04:10

Leif Andersen


I do miss not having a not= procedure (or as mentioned in @soegaard's comment), but not for the reasons you think.

All the numeric comparison operators are variadic. For example, (< a b c d) is the same as (and (< a b) (< b c) (< c d)). In the case of =, it checks whether all arguments are numerically equal. But there is no procedure to check whether all arguments are all unequal—and that is a different question from whether not all arguments are equal (which is what (not (= a b c d)) checks).

Yes, you can simulate that procedure using a fold. But still, meh.


Edit: Actually, I just answered my own question in this regard: the reason for the lack of a variadic procedure is that you can't just implement it using n-1 pairwise comparisons, unlike all the other numeric comparison operators. The straightforward approach of doing n-1 pairwise comparisons would mean that (≠ 1 2 1 2) would return true, and that's not really helpful.

I'll leave my original musings in place for context, and for others who wonder similar things.

like image 23
Chris Jester-Young Avatar answered Oct 17 '22 06:10

Chris Jester-Young