## tally and scalar functions

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !

### tally and scalar functions

This post is a follow-on to the one yesterday about scalar functions, scalar extension, and the conformance rules. Just how it is related will soon be clear.

First, an aperitif: In the 1960s, at the time of APL\360, how do you get the length of a vector as a scalar?

`      ''⍴⍴v      (⍴0)⍴⍴v      (⍳0)⍴⍴v      (⍴v)[0]      ×/⍴v    ⍝ shortest and obscure      0⊥⍴v    ⍝ shortest and obscurest`

From [Hui and Kromberg 2020]:
In A Programming Language [Iverson 1962, §1.5], the function μ (Greek mu) is the number of rows of a matrix and the function ν (Greek nu) is the number of columns of a matrix or the number of elements of a vector. These were combined and generalized in APL\360 into a single function ⍴, the shape of an array of any rank (dimensionality) as a vector result [Falkoff 1969]. (Falkoff quipped that he didn’t know any other Greek letters.)

It is useful to have a function which returns the number of major cells in an array, as a scalar, the tally or count function ≢⍵. Tally was first implemented in A in 1989 [Whitney 1989], added to J on Whitney’s suggestion [Hui et al. 1990], and more recently added to NARS2000 and Dyalog APL [Hui 2013c].
...

The effects of having this simple function ≢ are subtle and far-reaching or, rather, the effects of not having this simple function are subtle and far-reaching. For example, for calculating the average of an array, {(+⌿⍵)÷≢⍵} is superior to {(+/⍵)÷⍴⍵} or {(+⌿⍵)÷⍴⍵}, and likewise +⌿÷≢ is superior to +/÷⍴ or +⌿÷⍴ [Hui 2010f].

One speculates that the permissive and infelicitous treatment of singles in the conformability rules for the primitive scalar dyadic functions, + - × ÷ ⌈, etc., arose out of a desire for arguments involving ⍴ of a vector to give a result instead of an error.

strict: (⍴⍺)≡⍴⍵, or ⍺ or ⍵ is scalar
permissive: (⍴⍺)≡⍴⍵, or ⍺ or ⍵ is single

(A single is an array all of which dimensions are 1s, ∧⌿1=⍴⍵.)
Roger|Dyalog

Posts: 207
Joined: Thu Jul 28, 2011 10:53 am

### Re: tally and scalar functions

You may know CoSy evolved thru Arthur Whitney's K which follows Backus's Turing talk ( which I think set the stage for Ken's the next year ) observation a matrix was just a list of lists of equal length . So , the equivalent of ≢ , for which Arthur uses # , and I couldn't help my nostalgia from using ` rho , is the only fundamental count verb .

CoSy in an open to the x86 Forth uses whitespace as the prime delimiter of words , which makes it possible to make far more expressive names than either K or J which are also ( self ) restricted to the ascii keyboard .
The header of a CoSy list ( everything is a list ) is `( Type Count refCount )` . So the definition of ≢ , CoSy's ` i# is , at the naked x86 level , simply
Code: Select all
`: ≢ cell+ @ ;`

where ` @ , Forth's ` fetch , is just
Code: Select all
` 8B 00   mov eax,[eax] C3      ret `

Forth's simple RPN and the ` ' verb which returns the address of the following word ( ` returns the word following as a string ) makes a lot of constructions much simpler than in traditional APLs .

But , getting to the point , one of the decisions in CoSy I am most happy with is modulo indexing . There is no intrinsic notion of dimension , ie: rank , specifically 0 rank .
For example , with modulo indexing :
Code: Select all
`   10 _i iota0 1 2 3 4 5 6 7 8 9    R0 i( 1 -1 )i +i 1 0 3 2 5 4 7 6 9 8`

So there is never such a thing as a ` Index error . All trees are conformable with all other trees . Effectively , rather than being lists of lists they are rings of rings . ( NB: that _i is necessary to convert the naked 10 on the x86 stack to a 1 item CoSy list . And you can see I've never ` genericized most operations like add . Good learning exercise for someone . )

But there are a number of cases where ` singletons , lists of count 1 , are usefully special cased in a manner similar to scalars . In particular , if one argument is a singleton , it is generally convenient to disclose the result . I generally have variants for variations on the common task . For instance :
at _at at\ _at\
for selection with and without disclosure or conversion of raw integer on the stack .

Anyway modulo indexing generalizes scalar extension , and allows using negatives to index from the end of a list . My philosophy is the machine cycles are there to make my life evermore ` zipless . And it's remarkable how little it slows things down even with no special casing -- at least at my individual level of use .

Bob Armstrong

Posts: 16
Joined: Wed Dec 23, 2009 8:41 pm
Location: 39.038681° -105.079070° 2500m

### Re: tally and scalar functions

I had to look up the meaning of "infelicitous", and found the word "singularly" in the example very apt in the context of this topic:

unfortunate; inappropriate.
"his illustration is singularly infelicitous"

Thank you Roger for these insights which I find most illuminating. I really look forward to reading them.
Ray Cannon