Steps to a better BASIC — A Choice Method of Selection (reprint from Datalink)
Vector 3:1, page 93
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 93
In this issue’s extract from his series of articles introducing APL to BASIC users. Anthony Camacho shows how APL provides powerful selection processes. This article is reproduced with kind permission of Datalink magazine.
A Choice Method of Selection¶
Conditional operations without IF¶
In most microcomputer versions of BASIC, the logical value ‘true’ is represented by a one and ‘false’ by zero. A few treat any non-zero value for ‘true’ and/or set true variables to -1. This allows some elegant tricks. If you want an expression which puts B into A if x=y then
will fill the bill. Either the bracket X<>Y or the bracket X=Y will be a one so either A*1+0 or 0+B*1 will go into A.
If your computer renders PRINT 1=1 as -1 then the expression you need is
If what you want to do is to increment a counter until it reaches, say 50, then the expression is even neater:
The power of this kind of selection is greatly developed in APL, partly because it uses arrays of numbers of logical values and partly because it has some extra operators. In particular among these is the ‘compression’ operator, displayed as an oblique stroke. If the variable BOOLE is an array of ones and zeroes the same shape as an array VALUES containing any kind of item, then the expression ‘BOOLE/VALUES’ yields another array with as many items as there were ones in BOOLE. The items are, of course, the items from VALUES that corresponded to the ones in BOOLE.
For example suppose you wish to remove all punctuation characters from a line (or page or chapter) of text. In APL it is easy to construct an array of ones and zeroes the same length as the text with zeroes where the punctuation marks are and ones for all the other characters. This is done by using the membership operator which gives ones and zeroes as its result. The result is the same shape as the left argument and has a one whenever the item in the left array occurs in the right argument. Then compression of the original array gives the result you want.
TEXT←'DEMONSTRATION. HOW, MEMBERSHIP
: (∊); AND ?COMPRESS! (/) CAN¨
REMOVE PUNCTUATION'
TEXT ∊ '.,:;?!¨'
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
⍝ EXCHANGE 0 AND 1 WITH LOGICAL NOT
(~)
(~TEXT ∊ '.,:;?!¨')/TEXT
DEMONSTRATION HOW MEMBERSHIP (∊) AND
COMPRESS (/) CAN REMOVE PUNCT
UATION
A similar line will convert all lower case to upper case. First it converts the text to its numeric form; then creates an array with ones where there are lower case characters (they have codes from 97-122). This array is then multiplied by 32 and subtracted from the numeric form of the text. (The lower case code for each letter is 32 more than the code for the corresponding upper case letter). When the codes are converted back from numbers to letters they are all upper case.
⍝ PHR CONTAINS A SENTENCE IN UPPER A
ND LOWER CASE
PHR
Now is the time for all good men to
come to the aid of the SDP.
⍝ ⎕AVI CONVERTS CHARACTERS TO NUMBER
S AND NUMBERS TO CHARACTERS
⎕AVI(¯32×(N>96)∧N<123)+N←⎕AVI PHR
NOW IS THE TIME FOR ALL GOOD MEN TO
COME TO THE AID OF THE SDP.
You may feel this messing about with text is trivial. The examples were chosen because text arrays are naturally displayed horizontally and so the examples take less space than numbers in columns would do. But similar expressions are used daily by APL users to select, say, all the accounts whose credit limit is exceeded or all the stock items whose rate of turn is in the bottom quartile or even all the MPs who were elected with the support of less than a third of their electorate.
And of course any action the program can take could easily be made to depend upon such selections.