From APL Wiki
Revision as of 10:41, 11 September 2022 by Adám Brudzewsky (talk | contribs) (Text replacement - "<source" to "<syntaxhighlight")
Jump to navigation Jump to search

Under (), or Dual (<syntaxhighlight lang=apl inline>¨</source>), is a primitive dyadic operator which takes two function operands. It pre-processes its argument(s) with the monadic right operand, before applying the left operand on/between the result(s), then undoing the pre-processing.[1]


In dialects that support user-defined operators and invertible functions, it can be defined as (or similar): <syntaxhighlight lang=apl> _U_ ← {⍺←{⍵ ⋄ ⍺⍺} ⋄ ⍵⍵⍣¯1⊢(⍵⍵ ⍺)⍺⍺(⍵⍵ ⍵)}</source>


The following example solves a LeetCode problem which asks to add two numbers given as reversed digit lists: <syntaxhighlight lang=apl>

     2 4 3 +_U_(10⊥⌽) 5 6 4

7 0 8 </source> First, each argument is reversed, then decoded from base 10. The addition is performed. Finally, the result is encoded to base 10 and reversed.

Close composition

In SHARP APL and J, Under is implemented as a close composition, meaning that (using SHARP syntax) <syntaxhighlight lang=apl inline>f¨g</source> has the overall function rank of <syntaxhighlight lang=apl inline>g</source>. This allows <syntaxhighlight lang=apl inline>¨></source> to serve the purpose of Each from a nested APL. J uses <syntaxhighlight lang=j inline>&.</source> for the close form and <syntaxhighlight lang=j inline>&.:</source> for the rankless form.


The most widely known form of Under for much of APL's history was SHARP APL's Dual operator, written <syntaxhighlight lang=apl inline>¨</source> and defined as a close composition. It was presented by Ken Iverson and Bob Bernecky with the name "with" in 1980,[2] and implemented for only a small number of right operands in 1981.[3] The name "Dual" was used in Rationalized APL in 1983, and the full SHARP implementation using function rank was completed just after.[4] Iverson changed the name to Under for his 1987 A Dictionary of APL, and J uses this name as well. It includes both a close form <syntaxhighlight lang=j inline>&.</source> like SHARP and a non-close form <syntaxhighlight lang=j inline>&.:</source>.

However, Iverson had defined the Dual operator with glyph <syntaxhighlight lang=apl inline>⍢</source> in his 1978 paper Operators and Functions,[5] using a non-close form because function rank had not yet been invented. NARS featured this operator in 1981, preceding SHARP's limited implementation. Roger Hui proposed Under with the same definition as a potential addition to Dyalog APL at Dyalog '15, and it was added to both Extended Dyalog APL and dzaima/APL in 2018.

Structural Under was developed by Marshall Lochbaum around 2017,[6] and was included in Extended Dyalog initially and added to dzaima/APL in 2020. Later dzaima would implement <syntaxhighlight lang=apl inline>⍢×</source> and <syntaxhighlight lang=apl inline>⍢|</source> in the same spirit, now featured in dzaima/APL and dzaima/BQN. The language BQN has included structural and computational Under, using the glyph , since its initial design in 2020.

External links


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, Withe, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Name classCase convertUnicode convert
Operators SearchReplace