[ENH] Magnitude >> ?

Richard A. O'Keefe ok at cs.otago.ac.nz
Mon Feb 16 02:15:48 UTC 2004


"Lex Spoon" <lex at cc.gatech.edu> wrote:
	First, I don't like these coded results.

Zeroth, recall that although this started with an example of mine,
it wasn't a *recommendation* of mine.

	It irritates me every time I use C's strcmp.

There are two problems with strcmp:
(1) It isn't integrated into the language (not that there is a framework
    for it to be integrated into)
(2) It *DOESN'T* use coded results, so you can't use a switch () to see
    what you got.  You are given _some_ positive integer, _some_ negative
    integer, or zero, not +1, -1, 0.
Having said that, a good way to use strcmp() in C is
    #define cmp(s1, R, s2) (strcmp((s1),(s2)) r 0)
so that
    cmp(fred, ==, "foobar")
works perfectly.

	If simply comparing with < and > are inappropriate for
	the code in question, 

"Inappropriate" is not the issue.  The issue is that building
the entire comparison framework on #< (or any other one of the sacred six)
is intrinsically inefficient.  It leads to situations where you have to
repeat a comparison.  For small numbers, who cares?  For large strings
(filenames, say, differing only at the end, as filenames commonyl do),
it starts to matter.  For still other objects, it may matter a lot.

	then can't we come up with *some* symbolic
	representation?  #< and #> and #= would be one reasonable way to go,

That's exactly what Prolog's compare/3 does.  Haskell uses LT, EQ, and GT.
Eiffel's .three_way_comparison (also known as .compare) uses -1, 0, and 1.
O'Caml uses Less | Equal | Greater with the function called .cmp
Standard ML uses LESS | EQUAL | GREATER and function name compare.

	I'd prefer even more that we have three Comparison classes and that the
	results were LessThan, GreaterThan, and Equal.  Then, you can do more
	things with the result of a comparison, like sending them
	#isLessOrEqual.
	
That's where using -1, 0, 1 shines.  We don't *need* (x isLessOrEqual)
when we can write (x <= 0).  You really don't want something as basic
as a comparison requiring an extra full-weight message send.

Using #<, #=, #> would be fine, because (x < y) iff (x ? y) == #< .

	Second, why #? ?  I've never seen this symbol used that way.

Then you haven't seen enough programming languages.  In some ways the
ideal selector name is #<=>, but Squeak doesn't support three-character
binary selector names.

	In this case, how about a textual name like #comparison: ?
	
The nearest thing to a consensus amongst the programming languages I know
is "compare".  #comparison: grates; somehow it is the wrong part of speech.


But, I repeat, this is NOT, on my part, a proposal.  Magnitude *should*
have been based on three-way comparison right from the start, but it
wasn't.  There are far more urgent things for Squeakers to worry about.




More information about the Squeak-dev mailing list