apldyalog

# Boolean array in If guard

According to the Dyalog APL language reference

the test condition to the right of :If must return a single element Boolean value 1 (true) or 0 (false)

Is there any particular reason why the value is allowed to be an array rather than requiring it to be a scalar? Is it only for convenience? Do other other APL dialects which implements the If statement also allow non-scalar guards?

Solution

• (A scalar is a 0-dimensional array.)

A few more details to expand on Jürgen's answer:

It is very common to have 1-element vectors where a scalar is actually appropriate. This is especially true in APLs that do not have the monadic `≢` (Tally) function (including Dyalog prior to version 14.0), prompting people to use `⍴` (Shape) instead. For the same reason, allowing a 1-element array (singleton) is widely allowed in many contexts where, strictly speaking, a scalar would be required. This is known as singleton extension. Here are some examples of constructs that give a 1-element vector when one would actually want a scalar:

• Checking the length of a vector with `42=⍴myList`
• Picking the first element of an array with `1⍴myTable`
• Getting the second element of a 2-element vector with `1↓myPair`
• Computing the rank of an array with `⍴⍴myArray`

Indeed, it was traditionally quite awkward to get a scalar, and this leniency was therefore a very welcome convenience. Some APLs didn't (and still don't!) have `⍬` for the empty numeric vector, but `⍴` was lenient about types and allowed `''` so the shortest contruct was the unintuitive `''⍴expression`. Today we have `⊃` or `↑` as first (depending on dialect).

Before control structures were added to various APLs (some still don't have them!), the only flow control was `→` (Branch) which was defined as "go to the first line number in the array on the right, if any; do nothing if empty", allowing various computational methods to select branching destination. For example:

`````` →(0=⍴⍴myArray)/SCAL
⍝ code for non-scalar arrays
→ENDIF
SCAL:
⍝ code for scalars
ENDIF:
``````

The new control structures had to allow easy conversion of existing code and support common patterns:

`````` :If 0≠⍴⍴myArray
⍝ code for non-scalar arrays
:Else
⍝ code for scalars
:EndIf
``````

I don't have access to APL+, but APLX also allows 1-element vectors — not other singletons. As far as I know, it is unique to Dyalog to also accept higher-rank singletons.