J语言
引用

↑  Roger Hui. . 2004  [2022-11-05]. （原始内容存档于2019-12-20）. Ken and I had in mind to implement A Dictionary of APL [8] together with hooks and forks (phrasal forms) [20]. ……
The choice to implement forks was fortuitous and fortunate. We realized only later [32] that forks made tacit expressions (operator expressions) complete in the following sense: any sentence involving one or two arguments that did not use its arguments as an argument to an operator, can be written tacitly with fork and @:(compose) and [(left) and ](right) and constant functions. If @: were replaced by the equivalent special fork [: f g, then a sentence can be written as an unbroken train (sequence of forks). ……
Meanwhile, Ken was concerned about the usefulness of forks, and worked hard at finding examples of forks beyond those in Phrasal Forms [20]. After a while, it seemed that everything was a fork. The explanation lies in the proof of completeness for tacit definition [32]: if the root (last) function in a sentence is applied dyadically, then a fork is required to write the sentence tacitly.
↑  .   [2022-06-09]. （原始内容存档于2022-07-06）.
↑  Kenneth E. Iverson. . 1987  [2022-06-08]. （原始内容存档于2022-06-12）. 
u⍥v    Rank: mv lv rv    Upon; Upon
The monad u is applied to the result of v, that is:
  u⍥v ⍵ ←→ u v ⍵ ←→ u⍤v ⍵
⍺ u⍥v ⍵ ←→ u ⍺ v ⍵
↑  Kenneth E. Iverson. . 1987  [2022-06-08]. （原始内容存档于2022-06-12）. 
u⍤v    Rank: mv mv mv    On; On
Monad. In the simplest case u⍤v ⍵ is equivalent to u v ⍵. …… more generally, the rank of the derived function u⍤v is the rank of v; that is, the expression u v is applied to each of the cells of ⍵ relative to v. ……
Dyad. The left and right ranks of u⍤v are both the monadic rank of v. Therefore ⍺ u⍤v ⍵ is equivalent to (v⍺) u v ⍵.
↑  Kenneth E. Iverson. . 1987  [2022-06-08]. （原始内容存档于2022-06-12）. Withe (u⍩v) is similar to (u⍤v), but applies v only to the right argument:
⍺ u⍩v ⍵ ←→ ⍺ u v ⍵
  u⍩v ⍵ ←→ ⍵ u v ⍵
↑  Kenneth E. Iverson. . 1987  [2022-06-08]. （原始内容存档于2022-06-12）. 
u¨v    Rank: mv mv mv    Under; Under
This function is equivalent to composition (u⍤v) except that the function inverse to v is applied to the result of each cell. …… The function u¨v is often called “the dual of u with respect to v”, but the phrase “u under v” is probably better, suggesting that u is performed after preparatory work by v, and before the task is sewn up by reversing the effect of v. The expression u¨v is valid only if v possesses an inverse.
↑  Roger K. W. Hui, Morten J. Kromberg. . Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020  [2022-06-20]. （原始内容存档于2022-07-10）. One of the HOPL IV badges has an APL expression on it: ÷+/÷(e≠0)/e, the reciprocal of the sum of the reciprocals of the non-zero values of e. The expression computes the total resistance of components connected in parallel, whose resistance values are the vector e.
There is an alternative phrasing in modern APL: +⌿⍢÷e~0, sum under reciprocal (§3.5), without 0s. If arithmetic were extended to infinity (§4.6), in particular if ÷0 ↔ ∞ and ÷∞ ↔ 0, then the expression would simplify to +⌿⍢÷e, without the without 0 (~0).
↑  Kenneth E. Iverson. . 1978  [2022-06-21]. （原始内容存档于2022-06-24）. The power operator, denoted by ⍣, applies to a monadic function left argument f and an integer right argument k to produce the kth power of f in the following sense: f⍣k ↔ f f⍣k-1, and f⍣1 ↔ f. In particular, f⍣0 is the identity function and f⍣¯1 is the inverse of f. Moreover, f⍣_ denotes the limit of f, that is, the limiting function f⍣n for n large. Similarly, f⍣¯ denotes the limit of the inverse of f.
↑  .   [2022-10-23]. （原始内容存档于2022-12-05）.
↑  
Kenneth E. Iverson, Peter K. Wooster. . APL Quote Quad, Volume 12, Number 1, Proceedings of APL81, ACM. September 1981  [2022-06-29]. （原始内容存档于2022-06-29）. This paper proposes two related extensions to APL: the extension of assignment to allow a name F to be assigned to a derived function by an expression of the form F←+.x, and the introduction of a dyadic operator ∇ to apply to character arrays D and M so that D∇M produces an ambivalent function in which the dyadic case is defined by D and the monadic case by M. 

Kenneth E. Iverson. . 1983  [2022-06-19]. （原始内容存档于2022-07-22）. The proposed replacement for ⎕fx is a modification of the direct definition operator ∇ defined in [A Function Definition Operator], ……
A function produced by the operator ∇ may be assigned a name (as in f←m∇d or in a(f←m∇d)b), but it may also be used without assigning a name, as in y←''∇'⍺+÷⍵'/x.
↑  
John Scholes（英语：）.  (PDF). October 1996  [2022-06-30]. （原始内容存档 (PDF)于2021-10-05）. A Direct Function (dfn) is a new function definition style, which bridges the gap between named function expressions such as rank←⍴∘⍴ and APL’s traditional ‘header’ style definition. 

. 14 May 2020  [2022-06-30]. （原始内容存档于2022-07-03）. Explicit entities can be defined using direct definition. The digraphs {{ and }} are reserved for delimiters, and text found between {{ }} is taken to define a verb/adverb/conjunction. The text may be multiple lines long and may contain other embedded definitions. The part of speech of the defined entity is inferred from the words in it.
↑  .   [2022-10-13]. （原始内容存档于2022-10-10）.
↑  控制结构
↑  A. D. Falkoff（英语：）, K. E. Iverson.  (PDF). IBM. August 1968  [2022-07-09]. （原始内容存档 (PDF)于2021-10-27）. 

Table 3.9 shows the detailed definitions of transposition for a variety of cases. ……


Case⍴RDefinition

R←1⍉V⍴VR←V

R←1 2⍉M⍴MR←M

R←2 1⍉M(⍴M)[2 1]R[I;J]←M[J;I]

R←1 1⍉M⌊/⍴MR[I]←M[I;I]

R←1 2 3⍉T⍴TR←T

R←1 3 2⍉T(⍴T)[1 3 2]R[I;J;K]←T[I;K,J]

R←2 3 1⍉T(⍴T)[3 1 2]R[I;J;K]←T[J;K;I]

R←3 1 2⍉T(⍴T)[2 3 1]R[I;J;K]←T[K;I;J]

R←1 1 2⍉T(⌊/(⍴T)[1 2]),(⍴T)[3]R[I;J]←T[I;I;J]

R←1 2 1⍉T(⌊/(⍴T)[1 3]),(⍴T)[2]R[I;J]←T[I;J;I]

R←2 1 1⍉T(⌊/(⍴T)[2 3]),(⍴T)[1]R[I;J]←T[J;I;I]

R←1 1 1⍉T⌊/⍴TR[I]←T[I;I;I]

Table 3.9: TRANSPOSITION
↑  Roger Hui. . 22 May 2020  [2022-08-08]. （原始内容存档于2022-08-08）. Dyadic transpose, x⍉y, is probably one of the last primitives to be mastered for an APLer, but is actually straightforward to describe.
↑  
IBM.  (PDF). June 1976  [2022-07-02]. （原始内容存档 (PDF)于2019-09-26）. If P is any permutation of the indices of the axes of an array A, then P⍉A is an array similar to A except that the axes are permuted: the Ith axis becomes the P[I]th axis of the result. Hence, if R←P⍉A, then (⍴R)[P] is equal to ⍴A. For example:
      A←2 3 5 7⍴⍳210
      ⍴A
2 3 5 7
      P←2 3 4 1
      ⍴P⍉A
7 2 3 5
……
The monadic transpose ⍉A reverses the order of the axes of its argument. Formally, ⍉A is equivalent to (⌽⍳⍴⍴A)⍉A. 

上面的APL转置⍉运算规定中，不将多个轴映射到结果中的一个轴的简单示例，所对应的J语言代码：
   transpose=: {{(/: x) |: y}}
   a=: >:i. 2 3 5 7 
   $ a
2 3 5 7
   p=: 2 3 4 1 
   $ p transpose a
7 2 3 5
↑  Kenneth E. Iverson. . 1978  [2022-06-21]. （原始内容存档于2022-06-24）. The use of the indexing function will be illustrated in a proof of the useful identity i⍉j⍉a ↔ i[j]⍉a. We first state the definition of the transpose as follows: if k is any vector index of j⍉a then
      k⌷j⍉a ↔ k[j]⌷a
Then:
      k⌷i⍉j⍉a
      k[i]⌷j⍉a
      (k[i])[j]⌷a
      k[i[j]]⌷a
      k⌷i[j]⍉a 

Kenneth E. Iverson的上述二元转置定义，用J语言写为(<k){j<nowiki>|</nowiki>:a ↔ (<(/:j){k){a，而恒等式<nowiki>i|:j|:a</nowiki> ↔ <nowiki>(i{j)|:a</nowiki>的推导过程相应如下：

   (<k){i|:j|:a
   (<(/:i){k){j|:a 
   (<(/:j){(/:i){k){a
   (<k){(/:(/:j){(/:i))|:a
   (<k){(i{j)|:a
↑  Roger K. W. Hui, Morten J. Kromberg. . Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020  [2022-06-20]. （原始内容存档于2022-07-10）. Well, we don’t know what Ken Iverson’s favorite APL expression was or if he even had a favorite APL expression. But we can guess. From A History of APL in 50 Functions [Hui 2016a, §8]:
    ⊢ x←,1
       1
    ⊢ x←(0,x)+(x,0)
       1 1
    ⊢ x←(0,x)+(x,0)
       1 2 1
    ⊢ x←(0,x)+(x,0)
       1 3 3 1
The expression (0,x)+(x,0) or its commute, which generates the next set of binomial coefficients, …….
↑  Roger K. W. Hui, Morten J. Kromberg. . Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020  [2022-06-20]. （原始内容存档于2022-07-10）. What is being indexed is an array (of course) but the indices themselves (the “subscripts”) can also be arrays. For example [Hui 2016a, §4]:
    x← 3 1 4 1 5 9
    '.⎕'[x∘.>⍳⌈⌿x]
……
In the example, the 2-element character vector '.⎕' is indexed by a 6-by-9 Boolean matrix.
↑  
IBM.  (PDF). June 1976  [2022-07-02]. （原始内容存档 (PDF)于2019-09-26）. More generally, Q⍉A is a valid expression if Q is any vector equal in length to the rank of A which is "complete" in the sense that if its items include any integer N they also include all positive integers less then N. For example, if ⍴⍴A is 3, then 1 1 2 and 2 1 1 and 1 1 1 are suitable values for Q but 1 3 1 is not. Just as for the case P⍉A where P is a permutation, the Ith axis becomes the Q[I]th axis of Q⍉A. However, in this case two or more of the axes of A may map into a single axis of the result, thus producing a diagonal section of A as illustrated below:
……
      B←3 5⍴⍳15
      B
 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
      1 1⍉B
1 7 13
↑  Roger K. W. Hui, Morten J. Kromberg. . Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020. Quicksort works by choosing a “pivot” at random among the major cells, then catenating the sorted major cells which strictly precede the pivot, the major cells equal to the pivot, and the sorted major cells which strictly follow the pivot, as determined by a comparison function ⍺⍺. Defined as an operator Q:
   Q←{1≥≢⍵:⍵ ⋄ s←⍵ ⍺⍺ ⍵⌷⍨?≢⍵ ⋄ (∇ ⍵⌿⍨0>s)⍪(⍵⌿⍨0=s)⍪(∇ ⍵⌿⍨0<s)}
……
The above formulation is not new; see for example Figure 3.7 of the classic The Design and Analysis of Computer Algorithms [Aho et al. 1974]. However, unlike the pidgin ALGOL program in Figure 3.7, Q is executable, and the partial order used in the sorting is an operand, the (×-) and cmp¨ and cmp⍤1 in the examples above.
↑  Roger K. W. Hui, Morten J. Kromberg. . Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020  [2022-06-20]. （原始内容存档于2022-07-10）. Monadic ⍋ was originally defined only on numeric vectors, and was extended [Wooster 1980] to work on numeric arrays with higher rank. With that extension it has the distinction of being the first APL primitive function defined to work on major cells, before the term was invented or the importance of the concept realized. It was later extended to work on character arrays in Dyalog APL in 1982. More recently, ⍋ was extended in J to work with a TAO (total array ordering) [Hui 1996] on a suggestion by Whitney. TAO was taken up by Dyalog APL in 2018 [Brudzewsky et al. 2018]. The TAO also extends the left domain of ⍸. (The expression above for getting grade from sort requires TAO.)
↑  Roger K. W. Hui, Morten J. Kromberg. . Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020. Generate the sorted matrix of all permutations of ⍳⍵[Hui 2016a, §19; McDonnell 2003; Hui 2015b]. …… perm ⍵ obtains by indexing each row of the matrix ⍒⍤1 ∘.=⍨ ⍳⍵ by 0,1+perm ⍵-1. …… Putting it all together:
    perm ← {0=⍵:1 0⍴0 ⋄ ((!⍵),⍵)⍴(⊂0,1+∇ ¯1+⍵)⌷⍤1 ⍒⍤1 ∘.=⍨ ⍳⍵}
↑  .   [2022-09-27]. （原始内容存档于2022-09-27）.
1 2  .   [2022-11-01]. （原始内容存档于2022-11-01）.
↑  Roger K. W. Hui, Morten J. Kromberg. . Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020. An example of John Conway’s Game of Life [Gardner 1970] is obligatory with this operator. life below is due to Jay Foad, translated from an algorithm in k by Whitney [Hui 2017c]. It applies the rules of the Game of Life to the universe to create the next generation.
    life ← {3=s-⍵∧4=s←{+⌿,⍵}⌺3 3⊢⍵}
    ⊢ glider←5 5⍴0 0 1 0 0 1 0 1 0 0 0 1 1,12⍴0
……
    {'.⍟'[⍵]}¨ (⍳8) {life⍣⍺⊢⍵}¨ ⊂glider
……
In life, the derived function {+⌿,⍵}⌺3 3 computes the sum of each 3-by-3 square, moving by 1 in each dimension. The function {'.⍟'[⍵]} produces a compact display for a Boolean array.
↑  . Jay Foad offers another stencil life, translating an algorithm in k by Arthur Whitney: 
    life3 ← {3=s-⍵∧4=s←{+/,⍵}⌺3 3⊢⍵}    ⍝ Jay Foad
……
The algorithm combines the life rules into a single expression, wherein s←{+/,⍵}⌺3 3 ⊢⍵
(0) for 0-cells s is the number of neighbors; and
(1) for 1-cells s is the number of neighbors plus 1, and the plus 1 only matters if s is 4.
↑  Roger K. W. Hui, Morten J. Kromberg. . Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020  [2022-06-20]. （原始内容存档于2022-07-10）. Key is a monadic operator. In the dyadic case of the derived function ⍺ f⌸ ⍵, major cells of ⍺ specify keys for the corresponding major cells of ⍵, and f is applied to each unique key in ⍺ and the selection of cells in ⍵ having that key. In the monadic case of the derived function, f⌸⍵ ↔ ⍵ f⌸ ⍳≢⍵: f is applied to each unique key in ⍵ and the indices in ⍵ of that key.
Key was defined and implemented in J in 1989 or 1990 [Hui 2007] and in Dyalog APL in 2015 [Dyalog 2015; Hui 2020b]. It is motivated by the “generalized beta” operation in The Connection Machine [Hillis 1985, §2.6], but generalizes the generalized beta by accepting arrays of any rank, not just vectors, and by permitting any function, not just reductions (folds). Key is also cognate with the GROUP BY statement in SQL. ……
The following snippet solves a “Programming Pearls” puzzle [Bentley 1983]: given a dictionary of English words, here represented as the character matrix a, find all sets of anagrams. ……he algorithm works by sorting the rows individually (note the use of the rank operator ⍤(§3.1)), and these sorted rows are used as keys (“signatures” in the Programming Pearls description) to group the rows of the matrix. As the anagram example illustrates, other APL functions can be used to create the requisite keys.
↑  Roger K. W. Hui, Morten J. Kromberg. . Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020. Generate ⍵ random numbers selected from ⍳≢⍺ according to the weights ⍺, a vector of positive real numbers [Hui 2017d]. For example, if a←7 5 6 4 7 2 0.4 are the weights, then in a ran 1e6 the number 0 should occur roughly as often as 4(both with a weight of 7) and 3.5 times as often as 5(with a weight of 2). 
    ran ← {(0⍪+⍀⍺÷+⌿⍺)⍸?⍵⍴0}
……
The technique can be used to generate random numbers according to a probability distribution [Abramowitz and Stegun 1964, §26.8]. If a discrete distribution with values v and probabilities p, then v[p ran ⍵]. If a continuous distribution, convert it into a discrete one by dividing (¯∞,∞) into an appropriate number of intervals. The interval midpoints are the values; the probabilities are the differences of the cumulative distribution function at the interval end points.
The problem was solved in 1975 [IPSA 1975]; the current solution uses to advantage the extension of ?0 to generate a random number drawn uniformly from the open interval (0, 1) (suggested by Whitney) and the interval index function ⍸(§2.3).
↑  Roger K. W. Hui, Morten J. Kromberg. . Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020. The following is an example of interval index ⍸ (§2.3) and ⌸ working together to illustrate the central limit theorem, that the sum of independent random variables converges to the normal distribution [Hui and Iverson 2004; Hui 2016b, §F].
    t←¯1+{≢⍵}⌸(⍳51),(4×⍳50)⍸+⌿?10 1e6⍴21
……
t counts the number of occurrences for interval endpoints 4×⍳50, of 1e6 samples from the sum of ten repetitions of uniform random selection of the integers from 0 to 20. A barchart of t:
    .⎕'[(6e3×⊖⍳⌈(⌈/t)÷6e3)∘.≤t]
……
The derived function {≢⍵}⌸x counts the number of occurrences of each unique cell of x. The Dyalog APL and J implementations recognize particular useful operands for key, for example {≢⍵} and {f⌿⍵}, and implement those cases with special code (§9.3) for better performance.
↑  .   [2022-10-20]. （原始内容存档于2022-10-22）. 
.   [2022-09-20]. （原始内容存档于2022-09-20）.