Hook

From APL Wiki
Revision as of 02:34, 28 May 2022 by Marshall (talk | contribs) (→‎External links: BQN docs instead of tutorial)
Jump to navigation Jump to search
Hook operators in BQN

A hook is an asymmetrical form of function composition that first applies one of the composed functions to one argument, then applies the other function to one argument and the result. In J, a 2-train is a hook, while I adds the mirror image to give two functions (I has first-class functions but no operators) hook (h) and backhook (H). BQN uses two operators Before () and After (), which also serve the purpose of the Bind operator.

The hook as an operator first appeared in A Dictionary of APL as Withe. However, Iverson and McDonnell's paper Phrasal Forms, which introduced trains[1], proposed hook as the meaning of a 2-train, and this was soon included in J. This definition specifies that (F G) y is y F G y and x (F G) y is x F G y. However, Roger Hui later opined that this definition was better suited to a dyadic operator (which could be denoted h.) than an element of syntax,[2] and defined to 2-train to represent Atop instead when he led the introduction of trains to Dyalog APL. By that time, Dyalog had long had the Beside (originally called Compose) operator which is equivalent to x F h. G y, that is, the dyadic case. The monadic functionality can be achieved using Commute as F∘G⍨y and the full ambivalent function can be written as F∘G⍨⍨.

In I and BQN, there are two hooks in order to maintain symmetry: for example, BQN defines Before () to be the dyadic operator {(𝔽𝕨⊣𝕩)𝔾𝕩} ("𝔾's left argument comes from 𝔽") and After () to be {(𝕨⊣𝕩)𝔽𝔾𝕩} ("𝔽's right argument comes from 𝔾"). In the dyadic case these functions are identical to Reverse Compose and Beside respectively, but in the monadic case they differ because the argument is used twice: the second function application takes it as an argument directly in addition to the result of the first function application.

Like Reverse Compose, the two hooks can be used together to form a split-compose construct.

    3‿¯1‿4 ×⊸×⟜| ¯2‿¯7‿1
⟨ 2 ¯7 1 ⟩
Works in: BQN

This definition behaves differently that the Compose-based one when only one argument is given: in that case, it becomes a monadic 3-train.

The name "hook" was chosen based on the hook shape of a function call diagram such as the one below, taken from Phrasal Forms.

⍺(fg)⍵ ←→ ⍺fg⍵

     f
    / \
   ⍺   g
        \
         ⍵

See also

External links

References

  1. Ken Iverson and Eugene McDonnell. Phrasal forms at APL89.
  2. Roger Hui. Hook Conjunction?. J Wiki essays. 2006. Accessed 2021-02-08.
APL syntax [edit]
General Comparison with traditional mathematicsPrecedenceTacit programming (Train, Hook, Split composition)
Array Numeric literalStringStrand notationObject literalArray notation (design considerations)
Function ArgumentFunction valenceDerived functionDerived operatorNiladic functionMonadic functionDyadic functionAmbivalent functionDefined function (traditional)DfnFunction train
Operator OperandOperator valenceTradopDopDerived operator
Assignment MultipleIndexedSelectiveModified
Other Function axisBracket indexingBranchStatement separatorQuad nameSystem commandUser commandKeywordDot notationFunction-operator overloadingControl structureComment