<<   >>

0. Array Logic

(x>0)-(x<0)

Signum of x for real x , assuming the values 1 , 0 , or ¯1 according as x is strictly positive, 0, or strictly negative.

   x← 5 ¯2.7 0 6

   (x>0)-(x<0)
1 ¯1 0 1
 	
   x × (x>0)-(x<0)
5 2.7 0 6

The phrase (x>0)-(x<0) dates from the earliest days of APL, being found in section 1.4 of A Programming Language [8a]. Three decades later, the idea was adopted by Knuth [9], who wrote that “Iverson’s convention” or Iverson brackets led to improvements in exposition and technique. Some writers call these “data-driven conditionals” [10]; during a discussion between Phil Last, John Scholes, and myself it was suggested that they be called “array logic” [11].

The usefulness of array logic in APL is due to the following:

• Boolean functions have value 0 or 1 rather than true or false [12a].
Functions apply to entire arrays, as in, for example, +/x>100 to compute the number of elements of vector x greater than 100 [12b].
A simple function precedence (“right to left”).

Falkoff and Iverson explained the 0-1 definition in The Design of APL [0b] in characteristically plain but telling language:

A very general and useful set of functions was introduced by adopting the relation symbols < ≤ = ≥ > ≠ to represent functions (i.e., propositions) rather than assertions. The result of any proposition was defined to be 0 or 1 (rather than, say, true or false) so that it would lie in the domain of other arithmetic functions. …

The adoption of the relation symbols as functions does not preclude their use as assertions in informal sentences. For example, although one might feel compelled to substitute “x≤y is true” for “x≤y” in the sentence “If x≤y then (x<y)∨(x=y)”, there is no more reason to do so than to substitute “Bob is there is true” for “Bob is there” in the sentence which begins “If Bob is there then …”.