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.

- Dyalog APL: How to filter an array like filter()
- Dyalog APL: what is the problem with loops?
- How can I read one byte from stdin in Dyalog APL?
- Dyalog APL: Check if a field exists in JSON?
- How do I make sleep in Dyalog APL?
- Dyalog APL: convert JSON to a pure array
- Pass function as an argument in APL
- Is Execute unsafe in namespece paths?
- How provide default values when using ⌸ in Dyalog APL
- In Dyalog APL, can an Reduce N-Wise be a function or is it just an expression?
- Indexing with nested vectors in APL
- How can I get the last input expression in Dyalog APL?
- DYALOG APL, Connect to SQL Server using .NET and load a DataTable with data using SqlConnection
- Can someone explain this behavior for finding parent/child relationships of unnamed Namespaces?
- How to get column names with Dyalog APL after an SQL query?
- How do I POST raw data in Dyalog APL HttpCommand?
- Multi-line text values in Dyalog APL
- How can I use rich boxing when printing something in a function in Dyalog APL?
- Clearing the screen in Dyalog RIDE
- Resuming execution after conditional breakpoint in Dyalog APL
- Access same row/column in each array in nested arrays in Dyalog APL
- Is another way to write this APL pattern?
- Setting a conditional breakpoint in Dyalog RIDE
- Ride dyalog apl multiline dfn unpaired brace
- How to change the font in RIDE for Dyalog APL?
- How do I tell a running Dyalog session to accept RIDE connections?
- Boolean array in If guard
- how to print values between 2 numbers in APL?
- How to use compose operator in APL?
- What conditions does ]Save in Dyalog APL have?