From APL Wiki
Jump to navigation Jump to search

Tally () or Count is a primitive monadic function which returns the number of major cells in its argument. The Tally of an array is also the first element of its shape, or 1 if it is a scalar (since a scalar is its own major cell by convention). Tally counts the first axis rather than the last because the number of major cells is more useful in leading axis theory.


Tally can compute the length of a numeric vector or string.


It gives the length of the first axis in a higher-rank array. Tally applied to an array's shape gives its rank.

      5 4 3 21 'b' 3 'd'  
      ≢⍴5 4 3 21 'b' 3 'd'

The Tally of a scalar is always 1.



Tally returns the length of the first axis of its argument if it has any axes (that is, if it is not a scalar), and 1 otherwise. This can be modelled easily with Shape and First:

Tally  {(),1}

An alternative implementation is to count the major cells by turning each into a scalar 1 with the Rank operator, then adding them up:

Tally  + {1}¯1


Tally was introduced in A with the name "count" and symbol #. The same notation was carried forward to A+, as well as J following Arthur Whitney's suggestion. The primitive was present in NARS2000 by 2010, with the name "Tally" and symbol >[1]. The symbol for Tally was introduced in Dyalog APL 14.0, and quickly adopted by NARS2000. It was later added to GNU APL and has been included in many recent APLs based on Dyalog, such as ngn/apl, dzaima/APL, and APL\iv.

Before the addition of Tally (and Zilde), there were numerous ways to get the length of a vector as a scalar:

×/v    ⍝ shortest and obscure
0⊥⍴v    ⍝ shortest and obscurest

External links




  1. NARS2000 Wiki. Tally. Old revision: 2010-08-29.
APL built-ins [edit]
Primitive functions
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-beamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Case convertUnicode convert
Operators SearchReplace