Skip to content

Text Inequalities

Vector 4:1, page 103

programming techniques

Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 103

A domain error results if one tries to apply greater than or less than (with or without ‘or equals’) to character data. As character data is often sorted in dictionary order it seems legitimate to allow such tests on character data. This is not to suggest that the primitives should do this directly. Such comparisons are bound to be application dependent.

When extended grade is available it is very easy to write defined functions to do these tests. They may use the ⎕AV sequence or allow a collating sequence as an argument.

The example extends the idea by making multiple comparisons at one time (putting one set of lines into a matrix). To keep it simple I have left out code to force the same number of columns in the matrix A and vector B.

C←A GT B
C←⍋ ⎕AV ⍋ A ⍪ B
C←(¯1↓C)>¯1↑C

Line [1] catenates the vector B onto the end of A, then grades the new matrix and puts the result in C.

Line [2] compares the grade for line B with the grade for each other line. As the grades are numeric there is no problem with the comparison primitives. The result is boolean, with a 1 for each line of A that is greater than B. Since B is catenated onto the end of A lines of a that are equal to B will be sorted before B and so will give a zero in the result.

Here is another example, this time for greater than or equal to.

C←A GE B
C ← ⍋ ⎕AV ⍋ B ⍪ A
C ← (1↓C) > 1↑C

By catenating A onto B the lines that are equal in A will be graded after B and so will give the required 1 in the result.

This plan can easily be modified to allow a collating sequence other than ⎕AV or even to replace the dyadic grade with a user defined function if the APL in use does not have that feature built-in.

It would not be difficult to cater for scalars or vectors in either argument. Then character arguments can be used in expressions such as:

LESS THAN IS LIKE GE WITH < INSTEAD OF >
LESS THAN OR EQUAL TO IS LIKE GT WITH < INSTEAD OF >

A←(A LT B) ⌿ A

D ← ((A GE B)∧A LE C)⌿A