J语言

J语言，是一种阵列编程语言，由肯尼斯·艾佛森和許國華（英语：Roger Hui）於1990年代初發明。J语言是APL語言的一种方言，延续了APL鲜明的簡潔性，它在數學和统计学程式設計上十分高效，特別是在需要进行矩陣运算的场合。 J语言最初起步于肯尼斯·艾佛森在1987年发表的《APL字典》，它实现了其中至关重要的秩的概念。J语言提供隐式定义机制包括秩、钩子、叉子和多种函数复合（英语：function composition (computer science)），并介入了作为头等对象的动名词，用以建立控制结构，它常被作为隱式編程的典范之一。 J语言的运算符，承袭APL传统，没有优先级并且最右先行，2 * 3 + 4按照2 * (3 + 4)来运算。以历史上APL使用的典型符号为例，符号/被用来指示折叠函数foldr1，所以+/1 2 3等价于1 + (2 + 3)；在APL中，除法被表示为数学除号÷，它将减号和冒号一起重复打印（英语：overstrike）在EBCDIC和ASCII二者的纸质文本终端上；J语言使用%表示除法，是对除号的一种近似或提示。 為了避免APL使用特殊的字符而遇到的問題，J语言只需基本的ASCII字符集，但使用点号.和冒号:作为“屈折”。点号和冒号除了前导着空白字符的情况之外，都与紧前字符形成类似双字符组的短字。多数“基础”或“原始”的J单字，都充当数学符号，通过点号或冒号来扩展这些可用基本字符的含义。在其他语言中经常是成对的很多字符，比如[] {} "" `` <>，在J语言中被当作单独的字，或者在有屈折的时候，作为多字符字的单字符字根。 J语言不再支持从1968年的APL\360就有的[;]形式的方括号索引，转而支持叫做“来自”（from）的索引机制，它起源自Kenneth E. Iverson于1978年在《算子和函数》中提出的，依据基数解码定义，并用符号⌷表示的索引。 J语言承袭IBM APL\360采用了平坦阵列模型，不支持由NARS（嵌套阵列研究系统）于1981年介入，并被IBM APL2所采纳的嵌套阵列模型；J语言增加了Kenneth E. Iverson于1978年在《算子和函数》中提出的盒装数据类型，它由SHARP APL于1981年介入，并于1983年在I. P. Sharp协会（英语：I. P. Sharp Associates）研究报告《理性化APL》中，列入与APL2相比较的“限定子集”（RS）而著重强调。 J语言支持AVX2指令集进行SIMD运算。为了包装用面向对象编程语言开发的API和框架，J语言提供了层级命名空间机制，这里所有名字都存在于特定语境（locale）中，可以避免软件包之间的名字冲突，并能有效的用作基于类的面向对象编程的框架。 J语言解释器默认装载标准库。通过包管理器，还可以安装各种插件，如果是在管理员权限下安装的J语言解释器，则安装插件也需要同样的管理员权限。J语言拥有常规调试机制，还有叫做Dissect的可视调试器。除了科学计算和统计分析，它还被用于关系数据库管理系统如Jd、极限编程和网络性能分析。 2011年3月，J语言采用了GNU通用公共许可证版本3，从而成为自由和开源软件，人们还可以在Jsoftware的商业许可证下利用源代码。 J语言的文档包括在官网的NuVoc中，在其中将主要的字罗列为“J原语（英语：Language primitive）”，并使用颜色标示出它们分别的词类。早期的文档还有入门和字典。在J语言中的字，被识别为名词、动词、定语（副词和连词）、系词、标点、控制字。一个程序或例程，如果接受数据作为输入并产生数据作为输出，则被称为“动词”，与之相对，数据参数被称为“名词”。 动词有两种形式：只有右侧一个参数的一元（monad）形式，和有左右两侧参数的二元（dyad）形式，例如在-1中减号是一元动词，而在3-2中减号是二元动词。J语言预定义了很丰富的动词，它们都自动的作用于多种数据类型之上。用户定义的程序可以自行命名，并用在任何允许使用原始动词的地方。无论原始动词还是派生动词，它们的一元定义与二元定义，在很大程度上是独立的。 J语言可以写出非常精简的程序，特别是存在重度的对符号的函数重载，以至于一些编程者将它称为难以阅读的只写语言。在计算机的终端上执行ijconsole，即可进入J语言的REPL解释器界面。 J语言的“Hello, World!”程序： 'Hello, world!' Hello, world! 这个Hello World的实现反映了J语言的传统用法，就是把程序录入到J解释器会话中，显示出表达式结果。还可以准备J脚本来作为独立程序来执行，比如在Linux系统上，可以编辑如下内容的一个文本文件，并命名为比如test01.ijs： #!/usr/bin/ijconsole echo 'Hello, world!' exit '' 注意第一行的#!必须顶头，这里的echo和exit，是与Unix shell中同名命令功能类似的动词。然后在终端界面中执行这个文件： $ ijconsole test01.ijs Hello, world! $ chmod +x test01.ijs # 另一种执行方式，授予这个文件可执行权限 $ ./test01.ijs Hello, world! 在J语言中函数一般称为动词，例如定义一个叫做avg的动词，计算一序列数的平均： avg=: +/ % # avg 1 2 3 4 2.5 一元动词#“计数”（tally），总计阵列中项目的总个数。动词+“加”（plus）和副词/“插入”（insert），派生出的动词+/，合计这个阵列的项目的总和。二元动词%“除”（divide）将这个总和除以这个总个数。而用户定义的动词avg，用到了由连串（strand）的三个动词（+/、%和 #）构成的一个“叉子”（fork）。叉子(f g h) y↔(f y) g (h y)，这里的f、g和h指示动词，而y指示一个名词。 使用avg的一些例子： ]a=: ?. 20 $100 NB. 产生100以内20个随机整数的一个向量 94 56 8 6 85 48 66 96 76 59 33 72 63 1 89 52 17 20 9 65 avg a 50.75 4 avg\ a NB. 周期大小为4的移动平均 41 38.75 36.75 51.25 73.75 71.5 74.25 66 60 56.75 42.25 56.25 51.25 39.75 44.5 24.5 27.75 ]b=: ?. 4 5 $50 NB. 产生50以内20个随机整数的一个矩阵 44 6 8 6 35 48 16 46 26 9 33 22 13 1 39 2 17 20 9 15 avg b 31.75 15.25 21.75 10.5 24.5 avg"1 b NB. 应用avg于m的每个秩为1的子阵列 19.8 29 21.6 12.6 一元副词/“插入”（insert），接受位于它左侧的一个运算元，并产生将这个动词应用于其参数的每个项目之间的一个动词。就是说，+/是一个动词，定义为应用+于给它的参数的各个项目之间。计算移动平均用到的二元副词\“中缀”（infix），将作为数据参数的列表划分成一系列的指定大小的连续项目的子列表，将所修饰动词应用于其上，并将这些结果形成一个列表。 一元动词]“相同”（same），恒等于给它的单一右参数，常像这样用来在赋值之后显示变量的内容。一元动词?.“掷骰/固定种子”（roll/fixed seed），不同于一元动词?“掷骰”（roll），在生成数据参数项目所指定大小范围内的随机数之时，采用固定的种子。这里确定对矩阵按行还是按列进行平均，用到了连词"“秩”（rank），它在后面的定语章节和单独条目中论述。 二元动词i.“出现索引”（index of），和二元动词i:“最后出现索引”（index of last），在任何大小的阵列内查找匹配者，并返回它的位置索引，如果未找到匹配者，则返回这个阵列的大小。例如： a=: 3 1 4 1 5 9 a i. 1 2 NB. 找到1和2的第一次出现的索引 1 6 a i: 1 2 NB. 找到1和2的最后一次出现的索引 3 6 在J语言中，排序可以按APL传统的两步骤方式，使用一元动词/:“升序索引”（grade up）或\:“降序索引”（grade down），和用二元副词~“被动”修饰的二元动词{“出自”（from），二者连串（strand）形成的一个“钩子”来完成。一元钩子(f g) y↔y f (g y)；副词~“反身·被动”，其一元定义为f~ y↔y f y，二元定义为x f~ y↔y f x。J语言还提供专用的二元动词/:“上升排序”（sort up）或\:“下降排序”（sort down）。下面是用例： a=: 2 0 4 7 15 9 8 0 4 9 18 8 1 18 /: a NB. 产生参数阵列的升序索引 1 7 12 0 2 8 3 6 11 5 9 4 10 13 ({~ /:) a NB. 从参数阵列中按升序索引选取出各个项目 0 0 1 2 4 4 7 8 8 9 9 15 18 18 /:~ a 0 0 1 2 4 4 7 8 8 9 9 15 18 18 (a - 10) /: a _10 _10 _9 _8 _6 _6 _3 _2 _2 _1 _1 5 8 8 安装用来支持CSV文件的插件： load 'pacman' NB. 加载包管理器 'install' jpkg 'tables/csv' NB. 安装CSV文件插件 'showinstalled' jpkg '' NB. 查看已经安装插件 一个CSV文件简单用例： load 'tables/csv' NB. 加载CSV插件 a=: i. 2 3 a writecsv jpath '~/test01.csv' NB. 将一个阵列写入一个CSV文件 12 ]b=: readcsv jpath '~/test01.csv' NB. 从一个CSV文件读入一个盒子阵列 ┌─┬─┬─┐ │0│1│2│ ├─┼─┼─┤ │3│4│5│ └─┴─┴─┘ ]c=: makenum b NB. 尽可能的将盒子阵列转换成数值阵列 0 1 2 3 4 5 下面演示使用J语言编写在管道中的过滤器，例如，在具有隐式编程机制Unix管道的Linux系统中，建立如下内容的文本文件，并命名为比如filter01.ijs： #!/usr/bin/ijconsole load 'tables/csv' stdout makecsv 10 + makenum fixcsv stdin '' exit '' 然后在终端界面中执行如下命令行： $ cat test01.csv | ijconsole filter01.ijs 10,11,12 13,14,15 J语言支持三种简单类型： 数值 文字（字符） 盒装 其中数值有很多变种。J语言提供的唯一搜集（collection）类型，是任意维度的阵列。多数算法可以使用这些阵列来简洁的表达。 J语言的数值类型之一是“位”。位有两个值：0和1。位还可以形成列表，例如1 0 1 0 1 1 0 0，是8个位的列表。在语法上，J分析器将位当作一个字。空格字符被识别为字形成字符，它处在属于其他数值字的字符之间。 J语言支持任意长度的列表。J语言进一步的在这些位列表之上，支持所有常见二元运算，比如动词*.“与”（and）、+.“或”（or）、-.“非”（not）、|.“反转·旋转”（reverse·rotate）、|.!.f“移位”（shift）等。J语言还支持位的二维、三维等阵列。上面的运算同样运行在这些阵列之上。 其他数值类型包括整数（比如3、42）、浮点数（3.14、8.8e22）、複數（0j1、2.5j3e88）、扩展精度整数（12345678901234567890x）和（扩展精度）有理分数（1r2、3r4）。同位一样，它们可以形成列表或任意维度的阵列。同位的情况一样，运算可以在一个阵列的所有数值之上。下面例子展示π的前50位，超出了IEEE 754双精度浮点数的53位二进制尾数能精确表示的最大范围，这就要用到J语言的扩展精度整数： 0j15 ": o. 1 NB. π在双精度浮点数下精确值的位数 3.141592653589793 <.@o. 10x ^50 NB. π乘以扩展精度10的50次幂 314159265358979323846264338327950288419716939937510 这里采用一元动词o.“π乘以”（pi times），和一元动词<.“下取整”（floor）二者的复合，得到预期的结果。位的列表可以使用一元动词#.“基数2”（base 2）解码成整数。整数可以使用一元动词#:“反基数2”（antibase 2）编码为位的列表。 J语言还支持文字即字符类型。文字包围在撇号'之间，比如'a'或'b'。文字的列表，通过将多个字符用撇号包围起来的常规字符串约定来表示，比如'abcdefg'。在字符串内的''表示'字符本身。单个的文字，典型的是8位宽即单字节的ASCII字符，此外J语言还支持Unicode文字。 不支持在文字上的数值和布尔运算，但支持面向搜集的运算，比如旋转等。使用动词".“执行·数值”（do·numbers），将字节阵列转换成数值；使用动词":“缺省格式·格式”（default format·format），将数值转换成字节阵列。 盒装类型的值是0维标量，而不管所包含的是怎样的数据结构。使用一元动词<“盒装”（box），将数据放置入盒子中；使用一元动词>“打开”（open），打开盒子中取出其中数据。还可以通过二元动词;“链接”（link）建立盒子的列表，通过一元动词;“拆除”（raze）移除一层盒子的列表。盒子内可以装入其他盒子，还可以通过二元动词$“重制形状”（reshape）和二元动词#“计件复制”（copy）等操作盒子及其列表。 J语言的阵列，具有同质（homogeneous）的项目类型，例如列表1 2 3是整数的列表，尽管1还可以是一个位。这种类型问题，在极大程度上对于编程者是透明的。只有特定的特殊运算，会显露出在类型上的不同。例如列表1.0 0.0 1.0 0.0，对大多数运算，将被当作是完全同于列表1 0 1 0。 J语言支持数值稀疏阵列，通过它们的下标存储非零数值。这在非零数值相对很少的情况下，是有效率的机制。 J语言还支持类和对象，但是它们并非数据类型，实际上使用盒装文字来提及类和对象，它们实为命名语境和编号语境。 此外还有一个伪类型即内存映射文件，它将变量映射到文件系统中的持久文件。 下面的表格简要列出了常用词汇。如果含义中用了间隔号（ · ）分隔，通常前者是只有一个右侧参数的一元含义，后者是左右两侧都有参数的二元含义。列出的对应APL符号，是Dyalog等现代APL所采用的符号。 J语言的能力，很大程度上来自它的“定语”（modifier：修饰词），这个范畴包括“副词”和“连词”：这些符号接受名词和动词作为运算元（operand），并以指定方式应用这些运算元。定语都可以应用于任何动词，包括用户写的动词，用户可以写自己的定语。 J语言的二元动词有右结合性，或称为尽量长右作用域，即它有尽可能多的右参数。定语即算子有左结合性，或称为尽量长左作用域，即它有尽可能多的左运算元。如果表达式中存在定语即算子，首先应用这些定语，然后应用其生成的动词。 一元副词/“插入”（insert），副词\“前缀·中缀”（prefix·infix），副词\.“后缀·外缀”（suffix·outfix），和连词;.“剪切”（cut），指定参数的诸个规则或不规则子集，并在其上执行运算。在J语言实现中，前缀和（英语：Prefix sum）+/\、极小值<./\和极大值>./\运算，是典型的会对其进行速度优化的特殊组合。 副词~“反身·被动”（reflex·passive），其一元形式f~ y↔y f y，将提供给动词的右参数重复放置在左参数位置上；二元形式x f~ y↔y f x，将提供给动词的左右两个参数对换位置。 名词的秩（rank）是排布其原子所依据的轴的数目，即它的形状中项目的数目。动词的秩是它能够在其上直接运算的右（和左）名词参数的最高秩，典型的表示为三个原子的一个列表：一元秩 二元左秩 二元右秩。对副词和连词标示的秩，是所形成的动词的秩。 秩在特定动词和特定名词的上下文下，将名词的诸维，划分成前缀诸维的序列，称为框架（frame）；和后缀诸维的序列，称为单元（cell）。秩采用连词"“秩”来操纵，对应于APL符号⍤，它有三种形式：u"n“指定秩”（assign），m"n“常量动词”（constant），u"v和m"v“复制秩”（copy），这里的u、v表示动词运算元，而m、n表示名词运算元。正数动词秩，指示单元诸维的数目，负数动词秩，指示框架诸维的数目，_指示整体。 二元动词的左右参数的框架经常是匹配的，就是说二者有相同的形状，从而保证了它们有相同数目的单元。如果左右参数的框架不匹配，有三种可以运算的情况： 标量一致，如果指定了两参数中某一侧的框架为空，即秩为_，这一个整体单元被应用于另一侧参数的所有单元。 前缀一致，两参数中有一侧的短框架是另一侧长框架的前缀，短框架的每个单元，被应用于对应的长框架去掉前缀余下诸维形成的单元阵列的所有单元。 后缀一致，两参数中有一侧的短框架是另一侧长框架的后缀，这时需要以两侧加上相同正数增量的方式指定秩，使原短框架侧的框架为空，原短框架的诸维形成的这一个单元阵列，被应用于原长框架去掉后缀余下诸维形成阵列的每个单元阵列。 连词@:“在于”（at）、@“顶上”（atop）、&:“并列”（appose）、&“合成”（compose），是四种复合（composition）。J语言支持叫作“钩子”（hook）和“叉子”（fork）的隐形连词，二种隐形连词和四种复合连词，规定了如何将参数或将所饰动词应用于参数的结果，提供给所饰动词来进行应用的规则。下表列出它们的定义： 在上面表格中，mv=: 0{v b.0，lv=: 1{v b.0，rv=: 2{v b.0，这里的副词b.0给出动词v的三个秩。在应用四种复合连词形成新动词的表达式中，@:和&:，要对第一步运算的中间结果，按所在子表达式的秩或整个表达式的秩_进行汇集（assembly），并在有需要的情况下进行框架填充，然后在这个汇集成的框架内进行第二步运算；而@和&，直接在第一步运算的框架内，对中间结果进行第二步运算；在整个表达式求值结束时，最终结果在有需要的情况下要进行整体填充。 在x (u @ v) y中，一元u直接在二元v所划分的框架内进行自己的运算。在x (u @: v)"v y中，@:将二元v的运算结果，汇集成"v所指定的框架。在x (u & v) y，二元u直接在一元v所划分的两个框架内进行自己的运算。在x (u &: v)"mv y中，&:将一元v的两个运算结果，汇集成"mv所指定的框架。 下面例子展示四种复合的中间结果的单元差异： ] a =: >:i. 2 3 1 2 3 4 5 6 ] b =: 0.1*>:i. 2 0.1 0.2 a (< @: +) b ┌───────────┐ │1.1 2.1 3.1│ │4.2 5.2 6.2│ └───────────┘ a (< @ +) b ┌───┬───┬───┐ │1.1│2.1│3.1│ ├───┼───┼───┤ │4.2│5.2│6.2│ └───┴───┴───┘ a (; &: |) b ┌─────┬───────┐ │1 2 3│0.1 0.2│ │4 5 6│ │ └─────┴───────┘ a (; & |) b NB. 这里的框架仍是2 3 ┌─┬───┐ │1│0.1│ ├─┼───┤ │2│0.1│ ├─┼───┤ │3│0.1│ └─┴───┘ ┌─┬───┐ │4│0.2│ ├─┼───┤ │5│0.2│ ├─┼───┤ │6│0.2│ └─┴───┘ 当连词&的一个运算元是名词的时候，表示“粘上”（bond），它通过向二元动词固定提供其一个参数的值而产生一个动词： &派生的动词经常作为一元动词使用，即m&v y或u&n y；如果作为二元动词使用，即x m&v y或x u&n y，左参数表示应用这个派生动词于右参数的次数。需要确保通过&定义的一元动词，不出现在能够取用左右两个的参数的上下文中；如此定义的二元动词，也不应该出现在只能取用一个右参数的上下文中。需要注意m&v、v/和v\等的左参数的作用域，有时遇到其左侧的复合连词会产生并非预期的效果，经常需要将表达式整体加以圆括号包围。 J语言还提供连词&.:“底下”（under）和&.“对偶”（dual）。下面定义中的动词幂^:_1表示逆运算： @、&和&.合称为“紧密复合”（close composition）。现代APL中另有¨“每个”（each），f¨相当于J语言中的f &.>。例如： 1 2 + &.:> 0.1 0.2 ┌───────┐ │1.1 2.2│ └───────┘ 1 2 + &.> 0.1 0.2 ┌───┬───┐ │1.1│2.2│ └───┴───┘ 在J语言中，孤立的动词序列叫做“列车”（train）， e f g h意味着(e (f g h))，d e f g h意味着(d e (f g h))；以此类推，动词列车的一般模式(a b c ...)，依赖于动词的数目，在偶数时形式为(a (b c ...))，最外层是个钩子；而在奇数时形式为(a b (c ...))，最外层是个叉子；二者的内部都是可能有多层的嵌套的叉子。 叉子、@:再加上[和]，可以将很多常用复合写为列车。在惯用法([: f g)中，并不实际执行的隐式动词[:“遮帽”（cap），屏蔽了叉子的左分支，形成了等价于f @: g的特殊化叉子。 与现代APL如Dyalog等对照，复合连词@:对应于同秩连词共享APL符号⍤的“顶上”（atop），而&:对应于⍥“上方”（over），共享&符号的“粘上”，对应于APL中的“绑上”（bind），APL的“绑上”和“边上”（beside）共享符号∘，“边上”的一元形式同于⍤，而二元形式同于钩子。在《APL字典》中，@对应其⍥，而&列入秩连词⍤之内，钩子对应于符号⍩“枝条”（withe），&.对应于符号¨。 下面的简单例子是计算欧几里得范数，和生成数位与维度坐标一致的整数： ]d=: (1 1),(1 1 1),:(3 4) 1 1 0 1 1 1 3 4 0 norm=: %: @ (+/) @: *:"1 NB. 它可加圆括号为((%: @ (+/)) @: *:)"1 norm d 1.41421 1.73205 5 coor=: 10&#. @ > @ { @: (< @: >: @ i."0) coor 2 3 4 111 112 113 114 121 122 123 124 131 132 133 134 211 212 213 214 221 222 223 224 231 232 233 234 在coor中采用的圆括号包围，使得@:处在整个表达式的最外层，从而形成了两步骤运算；右侧的第一步是圆括号包围的子表达式，它的完全加圆括号（fully-parenthesized）形式为：(((< @: >:) @ i.)"0)；左侧的第二步是{与复合到其上诸运算构成的子表达式，它的完全加圆括号形式为：(((10 & #.) @ >) @ {)。一元动词{“目录汇编”（catalogue），应用在盒装列表的列表之上，是接受可变数目的变长参数的典型的动词。 下面通过对圆括号包围的子表达式加以变化，辨析秩连词和复合连词的特性。这里的动词]，划分开了给连词或副词的名词运算元，和给所生成的动词的名词参数： ]a=: < @: >: @ i."0 ] 2 3 4 NB. 运算式可加圆括号为((< @: >:) @ i.)"0 ┌───┬─────┬───────┐ │1 2│1 2 3│1 2 3 4│ └───┴─────┴───────┘ a -: < @: (>: @ i.)"0 ] 2 3 4 NB. 运算式可加圆括号为(< @: (>: @ i.))"0 1 a -: < @ (>: @ i."0) 2 3 4 NB. 运算式可加圆括号为< @ ((>: @ i.)"0) 1 < @ >: @ i."0 ] 2 3 4 NB. 运算式可加圆括号为((< @ >:) @ i.)"0 ┌─┬─┬─┬─┐ │1│2│ │ │ ├─┼─┼─┼─┤ │1│2│3│ │ ├─┼─┼─┼─┤ │1│2│3│4│ └─┴─┴─┴─┘ < @: (>: @ i."0) 2 3 4 NB. 运算式可加圆括号为< @: ((>: @ i.)"0) ┌───────┐ │1 2 0 0│ │1 2 3 0│ │1 2 3 4│ └───────┘ 下面的例子展示并联电阻电路计算： 1 R t o t a l = 1 R 1 + 1 R 2 + ⋯ + 1 R n {\textstyle {\frac {1}{R_{\mathrm {total} }}}={\frac {1}{R_{1}}}+{\frac {1}{R_{2}}}+\cdots +{\frac {1}{R_{n}}}} ，它可以如下这样表达： Rtotal=: +/ &.: % Rtotal 10 5 15 2.72727 连词^:动词幂”（power of verb），有两种形式： ^:n，是运算元为名词的“固定幂”。 ^:v，是运算元为动词的“动态幂”。 对于固定幂u ^:n，如果x缺席，u ^:n y在以y为运算对象的迭代中，将动词u应用n次；如果x存在，x u ^:n y在以y为运算对象的迭代中，将动词x&u应用n次。如果n是阵列，则按每个原子项目都执行一次动词幂，结果的框架为这个阵列的形状；如果n是取值为0或1的变量，则形成布尔值条件执行；如果n是_1，则进行u的逆运算；如果n是_，则意味着“收敛”（converge），即反复应用u直到结果不再变化。例如： (1+*&3) ^:0 1 ] 1 2 3 4 1 2 3 4 4 7 10 13 ' ' , ^:4 'abc' abc (1+*&3) ^:_1 ] 4 7 10 13 1 2 3 4 (-:@(]+%)) ^:_ &1 ] 0.25 3 25 NB. 以巴比伦方法即一种牛顿法特例来计算平方根 0.5 1.73205 5 对于动态幂u ^:v，如果x缺席，u ^:v y在以y为运算对象的迭代中，将动词u应用v y次；如果x存在，x u ^:v y在以y为运算对象的迭代中，将动词x&u应用x v y次。动词幂可以形成动态条件执行，这里的动词v必须总是产生布尔值结果，应用动词u当且仅当v返回1。进而u ^:v ^:_ y可以形成while循环构造，只要v返回1，就反复的执行u，直到v返回0，或者u将它的参数无变化的返回。例如： -&2 ^:(>&4) "0 ] 1 3 6 12 NB. 对大于阈值4的列表项目减去2 1 3 4 10 4 (0.25&*@[+(1-0.25)&*@]) ^:< "0 ] 1 3 6 12 NB. 对大于阈值4的列表项目在其超出部份上扣除25% 1 3 5.5 10 +&3 ^:(<&100) ^:_ "0 ] 2 3 100 NB. 只要列表项目小于100就对它加上3 101 102 100 下面的例子用来辨析秩指定与框架划分及汇集的性质，其中涉及的直接定义等内容可见于后面的定义章节： itemize=: ,: : {{,: ^:x ] y}}"(0 _) NB. 定义具有一元和二元两种形式的项目化扩秩运算 <@itemize~ i.3 ┌─────┬─────┬─────┐ │0 1 2│0 1 2│0 1 2│ └─────┴─────┴─────┘ $&.> <@itemize~ i.3 ┌─┬───┬─────┐ │3│1 3│1 1 3│ └─┴───┴─────┘ $ <"2@itemize~ i.3 3 1 $&.> <"2@itemize~ i.3 ┌───┐ │3 │ ├───┤ │1 3│ ├───┤ │1 3│ └───┘ $ <"_1@itemize~ i.3 3 3 $&.> <"_1@itemize~ i.3 ┌───┬─┬─┐ │ │ │ │ ├───┼─┼─┤ │3 │0│0│ ├───┼─┼─┤ │1 3│0│0│ └───┴─┴─┘ #&.>@<"_1@itemize~ i.3 ┌─┬─┬─┐ │1│1│1│ ├─┼─┼─┤ │3│ │ │ ├─┼─┼─┤ │1│ │ │ └─┴─┴─┘ $ 1 $ 0$0 0 # 0$0 0 $ <"1@itemize~ i.3 3 1 1 $ <"_2@itemize~ i.3 3 1 3 $ <"0@itemize~ i.3 3 1 1 3 $ <"_3@itemize~ i.3 3 1 1 3 在J语言中，动名词（gerund）是叫做“原子表示”的特殊盒子的一个列表，这种盒子可以像任何其他盒子一样使用，并可以最终转变回到要执行的动词。关于动名词的运算有： 连词`“连结动名词”（tie or gerund），建立动名词。 连词`:“唤起动名词”（evoke gerund），在加以运算元之后成为： `:6，将动名词转变成动词列车； `:0，将转变回来的这些动词分别单独应用，并将它们的结果收集入一个列表。 连词@.“议程”（agenda），m @. n从动名词m中，选择出第n个原子表示，将它转变回到动词并执行它。 下面是动名词简单示例和考拉兹猜想示例： grd =: * ` (+&2) NB. 建立一个动名词 grd NB. 显示原子表示，这里的符号'0'标识名词 ┌─┬─────────────┐ │*│┌─┬─────────┐│ │ ││&│┌─┬─────┐││ │ ││ ││+│┌─┬─┐│││ │ ││ ││ ││0│2││││ │ ││ ││ │└─┴─┘│││ │ ││ │└─┴─────┘││ │ │└─┴─────────┘│ └─┴─────────────┘ {. grd NB. 动名词可以像普通盒子一样操纵 ┌─┐ │*│ └─┘ grd `:6 i. 4 NB. 将动名词转换成动词列车来执行 0 3 8 15 grd `:0 i. 4 NB. 将动名词转换成并行执行的动词 0 1 1 1 2 3 4 5 Collatz=: -: ` (1+*&3) @. (2&|) NB. 考拉兹猜想的算式 Collatz "0 ] 1 2 3 4 5 6 7 4 1 10 2 16 3 22 >./@(Collatz^:(>&1)^:_"0@>:@?@$&1e6) 1000 NB. 取1000个在1e6内的随机数测试考拉兹猜想 1 J语言支持用户进行显式定义，和{{……}}形式的直接定义（英语：Direct function）。下面以五种复合作为显式定义的例子： at=: conjunction define u (v y) : u (x v y) ) atop=: conjunction def '(u at v)"v' beside=: conjunction define u (v y) : x u (v y) ) appose=: conjunction define u (v y) : (v x) u (v y) ) compose=: conjunction def '(u appose v)"(0{v b.0)' 下面以矩阵乘法作为直接定义的例子，并顺带演示秩连词的用法： a=: ? @ $&1000 @: >: 4?10 b=: |: a p=: a (+/ . *) b matmul=: {{x u@:v"(1 _) y}} (a (+/ matmul *) b) -: p 1 inner=: {{x (u@:v"1)"(1 _) y}} (a (+/ inner * 0&|:) b) -: p 1 outer=: {{x u@(v/"_1~ |:)~"(2 _) y}} (a (+/ outer *) b) -: p 1 revmul=: {{x (i.<:#$y)&|:@(u@:v"(_ 1)~ |:)~"(2 _) y}} (a (+/ revmul * 0&|:) b) -: p 1 APL传统上的将内积Pf.gQ解释为f/PgQ，J语言的矩阵乘法要求写为Pf/ .gQ，不隐含的为左运算元f附加一元/。转置也是有较大开销（英语：Overhead (computing)）的运算，不同的矩阵乘法算法（英语：Matrix multiplication algorithm）有不同的参照局部性。 matmul通过"(1 _)将u@:v的右参数从向量扩展为一般阵列，它用于右参数为行主序（英语：Row- and column-major order）阵列的情况。在向量与向量列表二者诸项之间逐对的进行乘积累加运算，是BLAS的标准算法。 inner采用了两向量之间的点积运算u@:v"1，它适宜直接用于右参数为列主序（英语：Row- and column-major order）阵列的情况。在右参数为行主序阵列之时，需要如例子代码这样，对右参数阵列进行二元转置0&|:，这里的0指示将第一轴安排至最后位置而其他轴保持原序前移。这种基于点积的实现，通常需要进一步加以循环镶嵌（英语：Loop nest optimization）。 outer可以看作matmul的变体，它首先对左参数阵列进行转置，然后进行多组的向量与一般阵列之间二元的张量积运算v/"_1，最后在各组结果的列表上进行u计算，每组运算之后参与其中的左右两阵列的元素不会被其他组的运算再次访问。 revmul同inner一样适宜直接用于右参数为列主序阵列的情况，但采用了同matmul类似的计算方法，它在乘法之前对左参数阵列和在乘法之后对结果要做转置，matmul与revmul的关系如同 ( A B ) T = B T A T {\displaystyle \,(\mathrm {AB} )^{\mathsf {T}}=\mathrm {B} ^{\mathsf {T}}\mathrm {A} ^{\mathsf {T}}\,} 。matmul一次性访问左参数阵列，反复多次访问右参数阵列；revmul一次性访问右参数阵列，反复多次访问左参数阵列。 在隐式定义中，递归定义可以不通过名字引用自身，转而使用动词$:“自引用”。例如递归的计算斐波那契数列： fibonacci=: 1: ` ($:@-&2 + $:@<:) @. (>&2) "0 : [: fibonacci >:i.9 1 1 2 3 5 8 13 21 34 _9:到9:是常量动词。动词[:“遮帽”，用在连词u : v“一元与二元定义”所应用的动词位置上，即充任了一元动词u或二元动词v，可分别在如下两种情况下报错：定义的是二元动词，却不适当的被用作一元动词；或定义的是一元动词，却不适当被用作二元动词。 在显式定义和直接定义中，提供了类似其他过程语言的控制结构。这里列出的是每个范畴内的代表性控制字： J语言的索引机制采用二元{“来自”（from）动词来完成，它的秩为0 _，它有两种形式，分别为左参数为索引阵列的主轴索引，和左参数为二层或一层盒装结构的逐轴索引，二者分别对应APL中，方括号内为单个轴的主轴索引选取，和方括号内为;分隔的多个轴的逐轴索引选取。 索引阵列的每个项目指定对主轴的项目单元的一个选取，将它们的结果再汇合为一个阵列。负值索引表示从末尾往前记数。在APL中，这种形式的索引被称为“来自”（from），也叫做“选取”（select）或幽默地称为“明智”（sane）索引，最早出现在SAX（SHARP APL for UNIX）对其@“来自”索引的扩展中，部份现代APL，将它表示为符号⊇。例如： i. 2 3 0 1 2 3 4 5 1 0 { i. 2 3 3 4 5 0 1 2 1 0 {"1 i. 2 3 1 0 4 3 (i. 2 3) { 'abcdefg' abc def _1 { 'abcdefg' g 逐轴选取可以形成子阵列，在现代APL中，它被表示为⌷，故而也被称为“扁方块”（squad：squish quad）索引（indexing）或就叫做“索引”（index）函数。装在二层盒装结构中的，是对应诸轴的一层盒装子结构的列表，其中每个一层盒装子结构内都是数值列表，它对应在此轴内一个或多个项目选择。默认全选使用名词a:“么点”（ace）指示，它是盒装空列表<0$0。在尾部的连续多个默认全选不需要写出。例如： i. 3 4 0 1 2 3 4 5 6 7 8 9 10 11 (<<1 2) { i. 3 4 4 5 6 7 8 9 10 11 (<1 2;0 2 3) { i. 3 4 4 6 7 8 10 11 (<a:;0 2 3) { i. 3 4 0 2 3 4 6 7 8 10 11 (<a:;0 2 3) ┌──────────┐ │┌──┬─────┐│ ││┌┐│0 2 3││ │││││ ││ ││└┘│ ││ │└──┴─────┘│ └──────────┘ 它还支持一层盒装结构，装在其中的是数值列表，它的元素指示每轴选取一个项目，如果单选了所有轴，则指定一个原子项目。一层盒装结构中的这个数值列表，等价于二层盒装结构中一层盒装的单一数值的列表。两种索引形式可以结合使用，即可以将盒装结构的阵列作为给{的左参数，它按这个阵列的形状汇合多个逐轴选取的结果。例如： (<1 2) { i. 3 4 6 ((<0 0),(<2 2),(<1 1)) { i. 3 4 0 10 5 (<0 0),(<2 2),(<1 1) ┌───┬───┬───┐ │0 0│2 2│1 1│ └───┴───┴───┘ 例如(<<1 2),(<a:;0 2 3)这样的选取是合法的，将形状不一致的选取结果汇合在一起，会导致结果值填充。 重排轴也叫做二元转置。APL的二元转置对轴次序的指定，类似于组合数学中置换的一行形式的柯西表示法，它被称为“可能是APL编程者最后掌握的原始运算之一”。J语言的二元转置对轴次序的指定，不涉及将多个轴映射到结果中的一个轴的情况，使用了普通的索引形式的置换向量。在置换向量的元素个数小于阵列轴的数目之时，J语言在置换结果中将其指定的诸轴安排在尾部，而其他轴保持原来相对次序前移。 针对向量的索引运算，是不加盒装的主轴索引。要访问一般阵列的特定原子项目，其选取向量需要一层盒装，从而对其进行逐轴索引。置换向量p所对应的逆向置换向量是/:p，对于两个置换向量p和q，则有/:p{q ↔ (/:q){/:p。 二元转置与索引机制有密切关联，它有两个重要性质： 两次连续的二元转置，可以变换成等价形式：p|:q|:A ↔ (p{q)|:A，即先将后者置换向量p对前者置换向量q进行置换，然后用结果的置换向量做一次二元转置。 对二元转置后的阵列，进行原子项目的选取，可以变换成等价形式：(<k){p|:A ↔ (<(/:p){k){A，即先用置换向量p的逆置换向量/:p，对选取向量k进行置换，然后用结果的选取向量来选取未转置阵列。 下面是演示例子代码： a=: ? @ $&1000 @: >: 4?10 n=: # @ $ a p=: ?~ n q=: ?~ n (/:/:p) -: p 1 ((/:p) { p) -: i. n 1 (/: p{q) -: (/:q) { /:p 1 (p |: q|:a) -: (p{q) |: a 1 k=: ? $ p |: a ((<k) { p|:a) -: (<(/:p){k) { a 1 下面例子形成帕斯卡三角的二项式系数的直接定义，并顺带展示基本的矩阵与盒装运算： pascal=: {{(0&, + ,&0) ^: y 1}}"0 pascal @ i. ] 5 1 0 0 0 0 1 1 0 0 0 1 2 1 0 0 1 3 3 1 0 1 4 6 4 1 ({. (+//. @: pascal @ i.)) 9 NB. 斐波那契数列 1 1 2 3 5 8 13 21 34 ((+/ . *) |:) @: pascal @ i. ] 5 NB. 帕斯卡矩阵 1 1 1 1 1 1 2 3 4 5 1 3 6 10 15 1 4 10 20 35 1 5 15 35 70 <"0 @ pascal @ i. ] 5 ┌─┬─┬─┬─┬─┐ │1│ │ │ │ │ ├─┼─┼─┼─┼─┤ │1│1│ │ │ │ ├─┼─┼─┼─┼─┤ │1│2│1│ │ │ ├─┼─┼─┼─┼─┤ │1│3│3│1│ │ ├─┼─┼─┼─┼─┤ │1│4│6│4│1│ └─┴─┴─┴─┴─┘ ]t=: < @ pascal @ i. ] 5 ┌─┬───┬─────┬───────┬─────────┐ │1│1 1│1 2 1│1 3 3 1│1 4 6 4 1│ └─┴───┴─────┴───────┴─────────┘ (<@#&a:"0@|.@i.@# ,&> }:@,@:(,.&a:@<"0)&.>) t ┌─┬─┬─┬─┬─┬─┬─┬─┬─┐ │ │ │ │ │1│ │ │ │ │ ├─┼─┼─┼─┼─┼─┼─┼─┼─┤ │ │ │ │1│ │1│ │ │ │ ├─┼─┼─┼─┼─┼─┼─┼─┼─┤ │ │ │1│ │2│ │1│ │ │ ├─┼─┼─┼─┼─┼─┼─┼─┼─┤ │ │1│ │3│ │3│ │1│ │ ├─┼─┼─┼─┼─┼─┼─┼─┼─┤ │1│ │4│ │6│ │4│ │1│ └─┴─┴─┴─┴─┴─┴─┴─┴─┘ 二项式系数也可以写成隐式定义形式： pascal=: ((0&, + ,&0)@] ^:[ 1:)"0 : [: 下面是基于二元副词/“形成表格”（table），制作条形图和散点图的简易图表例子: barChartH=: {&('.',u:16b2584) @ (>/ i.@(>./)) : [: barChartH 3 1 4 1 5 9 2 6 5 ▄▄▄...... ▄........ ▄▄▄▄..... ▄........ ▄▄▄▄▄.... ▄▄▄▄▄▄▄▄▄ ▄▄....... ▄▄▄▄▄▄... ▄▄▄▄▄.... barChartV=: {&('.',u:16b258c) @ (</~ |.@i.@(>./)) : [: barChartV 3 1 4 1 5 9 2 6 5 .....▌... .....▌... .....▌... .....▌.▌. ....▌▌.▌▌ ..▌.▌▌.▌▌ ▌.▌.▌▌.▌▌ ▌.▌.▌▌▌▌▌ ▌▌▌▌▌▌▌▌▌ scatterChart=: {&('.',u:16b2588) @ (=/~ >:@|.@i.@(>./)) : [: scatterChart 3 1 4 1 5 9 2 6 5 .....█... ......... ......... .......█. ....█...█ ..█...... █........ ......█.. .█.█..... 这里用到的Unicode方块元素字符也出现在IBM PC代码页437之中。 这里的scatterChart可以使用二元副词}“修改”（amend）来实现： scatterChart=: {&('.',u:16b2588) @ ((>./ , #) {{ 1 y } $&0 x}} (#~ <&9@{."1)@((-~ >./) ,. i.@#)) : [: APL的二元转置，在多个轴映射到结果中的一个轴的情况下，将其依次安排到前导位置上并进行对角线选取。下面基于一元动词I.“真值位置索引”，定义进行对角线选取的动词diag，它的左参数是布尔值列表，其中的真值1指示与其位置对应的轴，要依次安排在前导位置上并进行对角线选取，其他的假值0所对应的轴相对位置不变。 diag=: {{ s=. (I. , I.@:-.) @ ({.~ #@$) t=. <"1 @ (i.@(<./)@({. $) */ #&1@[) ({~ (+/x)&t) @ (|:~ x&s) y}} 这里局部定义了s，它的左右参数同于给diag的参数，它生成二元转置需要的置换向量，这是由要安排到前导位置上的那些轴的位置索引，和余下其他轴的位置索引串接而成。这里的{.~ #@$以右参数的诸轴数目选取左参数，用来在左参数的真值和假值的总数小于右参数的诸轴数目之时，对左参数填充上假值。 接着局部定义了t，它的左参数是给diag的布尔值列表中真值1的个数，右参数是要对其指定数目的前导轴进行对角线选取的阵列，它生成对角线选取所需要的一层盒装选取列表。这里的(<./)@({. $)选取出要进行对角线选取的前导诸轴的最小长度，用i.形成这个长度的整数数列，再用#&1@[形成其长度为左参数的全为1的列表，通过*/在二者之上形成表格矩阵。 最后的表达式先进行指定的二元转置，再对其结果进行相应的对角线选取。下面是简单用例： >:i. 3 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 diag >:i. 3 5 1 7 13 i. 2 3 2 0 1 2 3 4 5 6 7 8 9 10 11 1 1 diag i. 2 3 2 0 1 8 9 0 2 1 |: i. 2 3 2 0 2 4 1 3 5 6 8 10 7 9 11 1 0 1 diag i. 2 3 2 0 2 4 7 9 11 J语言提供的排序机制基于了稳定排序算法，下面的例子代码是快速排序的直接定义： cmp=: * @ - quicksort=: {{ if. 1 >: #y do. y return. end. s=. y u y {~?# y (u quicksort (s<0)#y),((s=0)#y),(u quicksort (s>0)#y)}} 这里定义一个动词cmp，它通过逐个做两个数的差并取其符号，得到取值为_1、0或1的平衡三进制值。cmp将作为左运算元传递给副词quicksort。 在quicksort的定义中，向局部变量s赋值的表达式，第一步随机选择支点（pivot）运算，首先计算?#y，生成在数据总个数范围内的随机数，接着在其上计算{~，选择出在随机数指定的位置上的支点值；它的第二步运算，将运算元u，应用到其左参数的数据列表，和右参数的支点值二者之上。 随后是串接分治运算结果，首先将平衡三进制值列表，分别与0做逐项的三分法比较，得到三个布尔值列表；然后以这种列表中的0和1作为件数，复制出数据列表的符合这个条件一个新的子列表，其中两个作为参数传递给递归调用进行排序。 下面的快速排序实现，展示了隐式编程，即将函数复合在一起，而不显式的引用任何变量，不提及要应用于其上的形式参数。这里将前面代码中向局部变量s赋值时所求值的表达式改为隐式定义，进而以作为钩子的第一步运算的方式，代入引用这个变量的表达式之中，并且采用钩子的参数复制机制消隐了形式参数y： cmp=: * @ - quicksort=: {{((($:@#~ <&0),(#~ =&0),($:@#~ >&0)) (u ({~ ?@#))) ^: (1<#) y}} cmp quicksort 2 0 4 7 15 9 8 0 4 9 18 8 1 18 0 0 1 2 4 4 7 8 8 9 9 15 18 18 提供给连词^:的左侧运算元，外层是个一元钩子，它将提供给它的单一右数据参数，重复放置在它的左数据参数位置上。这个外层一元钩子的第一步运算，是生成平衡三进制值列表的嵌套的二层一元钩子(u ({~ ?@#))；而外层一元钩子的第二步运算，将生成的三个子列表串接起来。生成三个子列表的表达式，以数据列表是作为左参数，以平衡三进制值列表作为右参数；这里的三个二元钩子首先生成布尔值列表，接着进行对换了左右参数位置的二元复制运算，最后它们中有两个通过自引用$:进行了递归调用。 这个定义中的$:是在这个副词的私有语境内调用的动词，所以不像前面直接定义那样需要加上运算元u以副词形式来调用。这里没有对字符串长度小于等于1的情况进行处理，这是因为迭代运算在条件不满足时返回初始值，也就是返回这个字符串本身。将cmp的表达式，代入定义中的左运算元u，就能定义出动词，同时也不再需要外在的采用直接定义的形式。 将一元钩子替代为左分支为]的叉子，形成的动词列车更具可读性： quicksort=: {{(] (($:@#~ <&0),(#~ =&0),($:@#~ >&0)) ] u ] {~ [:?#) ^: (1<#) y}} 下面的例子定义基于一元/:进行字符串比较的cmp： cmp=: -/ @ (-.@-: * /:@;) 'alpha' cmp 'beta' _1 'beta' cmp 'alpha' 1 'beta' cmp 'beta' 0 t=: ' the heart has its reasons that the reason does not know' ]words=: <;._1 t ┌───┬─────┬───┬───┬───────┬────┬───┬──────┬────┬───┬────┐ │the│heart│has│its│reasons│that│the│reason│does│not│know│ └───┴─────┴───┴───┴───────┴────┴───┴──────┴────┴───┴────┘ cmp&> quicksort words ┌────┬───┬─────┬───┬────┬───┬──────┬───────┬────┬───┬───┐ │does│has│heart│its│know│not│reason│reasons│that│the│the│ └────┴───┴─────┴───┴────┴───┴──────┴───────┴────┴───┴───┘ 在这个cmp定义中，/:@;先将两个字符串参数进行盒装串接，然后一元/:给出二者的升序索引，二者之间为升序或相同时为0 1，而二者为降序时为1 0；至此是升序还是相同仍需区分，-.@-:判断两参数是否为“不相同”，不相同时为1，而相同时为0。这里的副词;._1“自有区间”，使用字符串的第一个字符作为分隔符，对字符串进行划分并去除分隔符，然后应用所修饰的动词于这些子字符串之上。 下面的例子给出n个项目的所有置换的有次序的矩阵。首先定义名词p3，它是置换长度为3的全排列矩阵： ]p3=: (i.@! A. i.) 3 0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0 这里用到了动词：A.“易位词”（anagram），x A. y的左参数x，指定了长度为#y的所有置换中给特定一个置换的编号，其对应的置换向量在组合数学中被称为逆序向量，据此编号置换y的项目。这里求全排列的数目，用到了一元动词!“阶乘”。接着在p3的基础上，实现置换长度为4的全排列矩阵： =/~ @ i. ] 4 NB. 生成4×4单位矩阵 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 \:"1 @ (=/~) @ i. ] 4 NB. 生成第1列为0 1 2 3，每行后3个元素升序的4×4“奇妙”矩阵 0 1 2 3 1 0 2 3 2 0 1 3 3 0 1 2 0&,. @: +&1 p3 NB. 对3元素全排列的每个元素增1，并在每个排列头部添0 0 1 2 3 0 1 3 2 0 2 1 3 0 2 3 1 0 3 1 2 0 3 2 1 perm0=: (0&,.@:+&1 p3) {"(_ 1) \:"1@(=/~)@i. <"_1 perm0 4 ┌───────┬───────┬───────┬───────┐ │0 1 2 3│1 0 2 3│2 0 1 3│3 0 1 2│ │0 1 3 2│1 0 3 2│2 0 3 1│3 0 2 1│ │0 2 1 3│1 2 0 3│2 1 0 3│3 1 0 2│ │0 2 3 1│1 2 3 0│2 1 3 0│3 1 2 0│ │0 3 1 2│1 3 0 2│2 3 0 1│3 2 0 1│ │0 3 2 1│1 3 2 0│2 3 1 0│3 2 1 0│ └───────┴───────┴───────┴───────┘ $ perm0 4 4 6 4 这里的{"(_ 1)，其左侧是从6×3全排列矩阵加工而成6×4选取矩阵，用它分别对其右侧的4×4“奇妙”矩阵的每一行进行主轴索引运算，即将选取矩阵每行第1个元素保持为“奇妙”矩阵此行的第1个元素，而每行后面3个元素是对“奇妙”矩阵此行后面3个元素的排列。这一步写出的排列动词的结果是4×6×4的三维阵列，将它重制形状为24×4全排列矩阵： perm1=: (,~ !) $ ,@((0&,.@:+&1 p3) {"(_ 1) \:"1@(=/~)@i.) $ p4=: perm1 4 24 4 二元动词$的左参数的值为(!y) , y，这里的y是给排列动词的数据参数；而它的右参数是用一元动词,将三维阵列散开后形成的一个向量。 然后将其中的p3，替代为递归的自引用$:@-&1；并通过名词秩，形成常量动词(1 0$0)"_，设置长度为0时的基础值为1 0$0： perm=: (1 0$0)"_ ` ((,~ !) $ ,@(0&,.@:+&1@$:@-&1 {"(_ 1) \:"1@(=/~)@i.)) @. (>&0) : [: (perm 4) -: p4 1 $ perm 0 1 0 $ perm 1 1 1 invertVec=: I.@(, -:"1 perm@#) : ({ perm) 1 _1 invertVec 4 0 1 3 2 3 2 1 0 invertVec 3 2 1 0 23 最后将递归形式改为迭代形式，可采用连词F..“单结果正向折叠”（fold single forward），使用它需要事先安装插件dev/fold。这个连词所形成的动词，从左至右遍历右参数列表，将其项目逐个作为动词运算元所见到的左参数；它的左参数是迭代对象的初始值，动词运算元所见到的右参数是迭代对象： load 'dev/fold/foldr' perm=: (1 0$0)&(]F..((,~ !)@[ $ ,@(0&,.@:+&1@] {"(_ 1) \:"1@(=/~)@i.@[)))@:(>:@i.) : [: 如果不采用连词F..，可以基于一元副词/“插入”，自行实现正向折叠算子： foldl=: {{m"_ ` (v&:>/@,&(<m)@(<"_1@|.)) @. (>&0@#)}} perm=: (1 0$0)foldl((,~ !)@[ $ ,@(0&,.@:+&1@] {"(_ 1) \:"1@(=/~)@i.@[))@:(>:@i.) : [: 在J语言中，提供了二元副词;.±3“子阵列”（subarrays），它是;.“剪切”（cut）的三种形式之一，也被称为密铺（tessellate）或镶嵌（tile）。x(u;._3)y应用动词u于由x指定的y的有相同形状的每个正规镶嵌之上。;.3与之类似，但不丢弃结果中不完整的镶嵌。 现代APL所使用的二元算子⌺“模板（英语：Stencil (numerical analysis)）”（stencil），在边缘的处理上不同于J语言的镶嵌，它要求镶嵌子阵列每个轴的中心，在长度为奇数时是y的元素，在长度为偶数时在其元素之间，并且用填充（fill）元素填满超出的部份，它的缺省移动步长是1。下面在;._3的基础上，利于二元动词{.“采取”（take）的填充特性，定义一个stencil实现： stencil=: {{ p=. 1&,:`|.@.(>&1@#@$) n r=. <.-:<:{: p t=. -r+s=. r+$y p u;._3 t&{.s&{. y}} 这个定义只提供一个右名词运算元n，它是镶嵌子阵列的规定矩阵，不提供同每个子阵列对应的诸轴填充数目作为左名词运算，APL⌺算子提供它，意图在需要时籍此移除填充。这里的镶嵌规定矩阵定义，其第1行是子阵列的每轴长度，第2行是每轴的移动步长，这个行次序与;.±3的规定相反。这里的p是给;._3的镶嵌规定矩阵，r是每轴在头部和尾部的填充数量，s是正值控制尾部填充，t是负值控制头部填充。在下面的简单用例中，数值阵列的填充元素是0： ]d=: 4 4 $ >: i. 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 < stencil 2 3 d ┌─────┬─────┬─────┬─────┐ │0 1 2│1 2 3│2 3 4│3 4 0│ │0 5 6│5 6 7│6 7 8│7 8 0│ ├─────┼─────┼─────┼─────┤ │0 5 6│5 6 7│6 7 8│7 8 0│ │0 9 1│9 1 2│1 2 3│2 3 0│ ├─────┼─────┼─────┼─────┤ │0 9 1│9 1 2│1 2 3│2 3 0│ │0 4 5│4 5 6│5 6 7│6 7 0│ └─────┴─────┴─────┴─────┘ 下面实现康威生命游戏，它是基于Moore（英语：Edward F. Moore）邻域（英语：Moore neighborhood）的一种细胞自动机: life=: {{3=s-y*.4=s=. (+/@,) stencil 3 3 ] y}} glider=: ".;._2 noun define 0 0 1 0 0 1 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 ) life glider 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 {&('.',u:16b2596) &.> (i.8) {{life ^:x ] y}} &.> <glider ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ │..▖..│.▖...│..▖..│.....│.....│.....│.....│.....│ │▖.▖..│..▖▖.│...▖.│.▖.▖.│...▖.│..▖..│...▖.│.....│ │.▖▖..│.▖▖..│.▖▖▖.│..▖▖.│.▖.▖.│...▖▖│....▖│..▖.▖│ │.....│.....│.....│..▖..│..▖▖.│..▖▖.│..▖▖▖│...▖▖│ │.....│.....│.....│.....│.....│.....│.....│...▖.│ └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ 这里的实现算法，将生命游戏规则合并入一个单一表达式中，它由Arthur Whitney提出。算法的第一步骤用+/@,合计每个格点及其周围格点中1数目，并将它局部保存在s中。第二步骤将为前面的结果与4做相等比较，再与当前状态y做逻辑与运算，只有在当前格点为1，而且周围格点合计为3的情况下，当前格点得到真值1。第三步骤用s减去前面的结果真值，再与3做相等比较。在这个最终结果中，当前格点得到真值1有两类情况：⑴它在s中的值为4，并减去了真值1，这就是第二步骤运算所选定的情况；⑵它在s中的值为3，并减去第二步骤运算在这种情况下必然得出的假值0，这又可细分为两种情况：当前格点为0，而且周围格点合计为3；或者当前格点为1，而且周围格点合计为2。 这个算法可以写为隐式定义形式： life=: (=&3@] +. (*. =&4)) (+/@,) stencil 3 3 : [: 在J语言中，实现以递推关系定义的序列，避免出现 O ( n 2 ) {\displaystyle \,O(n^{2})\,} 的时间复杂度，可以采用连词F:.“多结果正向折叠”（fold multiple forward），使用它需要事先安装插件dev/fold。这个连词所形成的动词，不输出左参数的初始值，如果提供的右参数为空值，它的输出为空值，否则它输出每次迭代的结果。上例中的迭代算式可以改写为： load 'dev/fold/foldr' {&('.',u:16b2588) &.> (< , <F:.(life@])&(7$0)) glider 如果不采用连词F:.，可以自行实现输出中间结果的正向折叠算子： foldlist=: {{ if. 1 > # y do. '' return. end. r=. '' [ a=. x for_i. y do. r=. r , u a=. i v a end. r }} {&('.',u:16b2588) &.> (< , <foldlist(life@])&(7$0)) glider 在这里的foldlist中，将r=. '' [ a=. x改为r=. u a=. x，则(< , <foldlist(life@])&(7$0))可改为<foldlist(life@])&(7$0)。 在J语言中，提供了二元副词/.“键分组”（key），它按左参数中的唯一键，对右参数进行分组（group）或称为分区（partition），并将所修饰动词应用到这些分组之上。分组次序同于一元动词~.“唯一值”处理结果的次序，它除去匹配前面出现过的项目的任何项目。下面在它的基础之上，定义对应现代APL使用的二元算子⌸“键分组”（key）的一个副词，它将所修饰动词应用在唯一键和相应的分组二者之上，它还具有一元和二元两种形式： key=: {{ (<"0 ~. y) u (y </. i.#y) : (<"0 ~. x) u (x </. y)}} 它的二元形式的左参数含有唯一键，而右参数是要分组的数据；它的一元形式的右参数含有唯一键，所分组的是右参数诸项目的索引值。下面是这个键分组副词所适用的动词的简单示例： x=: 'Mississippi' [ key x ┌─┬─┬─┬─┐ │M│i│s│p│ └─┴─┴─┴─┘ ] key x ┌─┬────────┬───────┬───┐ │0│1 4 7 10│2 3 5 6│8 9│ └─┴────────┴───────┴───┘ ,. key x ┌─┬────────┐ │M│0 │ ├─┼────────┤ │i│1 4 7 10│ ├─┼────────┤ │s│2 3 5 6 │ ├─┼────────┤ │p│8 9 │ └─┴────────┘ ([ ,. #&.>@]) key x ┌─┬─┐ │M│1│ ├─┼─┤ │i│4│ ├─┼─┤ │s│4│ ├─┼─┤ │p│2│ └─┴─┘ 下面的例子通过键分组来找到一个单词列表中的易位词： ]a=: <;._1 ' pats spat teas sate taps etas past seat eats tase star east seta' ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐ │pats│spat│teas│sate│taps│etas│past│seat│eats│tase│star│east│seta│ └────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘ /:~ &.> a ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐ │apst│apst│aest│aest│apst│aest│apst│aest│aest│aest│arst│aest│aest│ └────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘ (]key~ /:~&.>) a ┌─────────────────────┬─────────────────────────────────────────┬──────┐ │┌────┬────┬────┬────┐│┌────┬────┬────┬────┬────┬────┬────┬────┐│┌────┐│ ││pats│spat│taps│past│││teas│sate│etas│seat│eats│tase│east│seta│││star││ │└────┴────┴────┴────┘│└────┴────┴────┴────┴────┴────┴────┴────┘│└────┘│ └─────────────────────┴─────────────────────────────────────────┴──────┘ 在J语言中，提供了二元动词I.“区间索引”，它左参数x必须是有次序的，从而定义了1+#x个区间，除了最后一个之外的每个区间，都含有并结束于x的一个项目，而最后一个结束于正无穷，第一个开始于负无穷；它应用于右参数y，给出y所位于的区间的索引。 下面的例子产生符合指定离散概率分布的随机数列表，这里定义了动词ran，它依据左参数x给出的正实数向量中的这些权重，从i.#x中选取出由右参数y指定个数的随机数列表： ran=: [: : ((I.~ (+/\ % +/))~ ?@$&0) wt=: 7 5 6 4 7 2 0.4 # t=: wt ran 1e6 1000000 10 {. t 0 1 1 5 0 1 3 4 4 0 ] r=: wt (+/@(=/ i.@#)~ % #@]) t NB. 实测的出现比率 0.222618 0.159083 0.19152 0.127394 0.222795 0.06378 0.01281 ] p=: (% +/) wt NB. 期望的出现概率 0.22293 0.159236 0.191083 0.127389 0.22293 0.0636943 0.0127389 0j6 ": r - p _0.000312 _0.000153 0.000437 0.000005 _0.000135 0.000086 0.000071 这里首先通过?@$&0，生成指定数目的在区间(0,1)中的随机浮点数，它也可以写为等价的?@($ 0:)。然后在叉子+/\ % +/中，使用一元副词\“前缀”修饰动词+/，从而计算权重向量的前缀和（英语：Prefix sum），再用前缀和除以总和得出累积分布函数。最后通过区间索引，在有随机浮点数落入特定区间的时候，生成这个区间对应的随机整数。 动词ran的表达式是个二层二元钩子，外层钩子的第一步运算应用到右参数上，它的第二步运算即内层钩子，整体修饰了二元副词~“被动”而对换了两个参数的位置。内层钩子的第一步运算+/\ % +/所应用的右参数实际上是外层钩子的左参数，它的第二步运算I.修饰了~，从而将它所面对的内层钩子的左右两参数，再次对换回到外层钩子即整体表达式原先的位置上。 下面是将区间索引和键分组结合起来的例子，演示了林德伯格（英语：Jarl Waldemar Lindeberg）－莱维中心极限定理: histogram =: {{ {&('.',u:16b258c) @ ((|.i.x)&(</)) @ (>.@*&x) @ (% >./) y}} summary=: {{ l=. 0 [ r=. 1 -&1@#/.~ @ ((i.x)&,) @ ((l+(}.i.x)*%&x(r-l))&I.) y}} sampleMean=: {{%&m @ (+/) @ (m&, $ v@*&m) y}} 24&histogram @ (80&summary) @ (10 sampleMean (?@$&0)) 1e6 ......................................▌▌▌▌...................................... .....................................▌▌▌▌▌▌..................................... ....................................▌▌▌▌▌▌▌▌.................................... ....................................▌▌▌▌▌▌▌▌.................................... ...................................▌▌▌▌▌▌▌▌▌▌................................... ..................................▌▌▌▌▌▌▌▌▌▌▌▌.................................. ..................................▌▌▌▌▌▌▌▌▌▌▌▌.................................. .................................▌▌▌▌▌▌▌▌▌▌▌▌▌▌................................. .................................▌▌▌▌▌▌▌▌▌▌▌▌▌▌................................. ................................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌................................ ................................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌................................ ...............................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌............................... ...............................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌............................... ..............................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌.............................. ..............................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌.............................. .............................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌............................. ............................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌............................ ............................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌............................ ...........................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌........................... ..........................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌.......................... .........................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌......................... ........................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌........................ ......................▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌...................... .........▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌........ lineChart=: {{ s=. (|.i.x)&(</) @ (>.@*&x) @ (% >./) t=. >&0 @ (2&(-/\)) @ (2&(-~/\))"1 @ ,.&1 @ (1&,.) {&('.',u:16b2588) @ (2&(-~/\)@(0&,) +. t) @ s y}} 24&lineChart @ (+/\) @ (80&summary) @ (10 sampleMean (?@$&0)) 1e6 ....................................................████████████████████████████ ..................................................██............................ ................................................██.............................. ...............................................█................................ ..............................................█................................. ............................................██.................................. ............................................█................................... ...........................................█.................................... ..........................................█..................................... .........................................█...................................... ........................................█....................................... .......................................█........................................ .......................................█........................................ ......................................█......................................... .....................................█.......................................... ....................................█........................................... ...................................█............................................ ...................................█............................................ .................................██............................................. ................................█............................................... ...............................█................................................ .............................██................................................. ...........................██................................................... .........██████████████████..................................................... 这里定义的summary局部赋值了两个局部变量l和r，可以将这两个局部赋值去掉，并将这两个局部变量的出现替代为运算元m和n，如此对它的调用将变成(80&(0 summary 1))这样的形式。接下的代码结合前面两个例子，采用了不同于上例连续型均匀分布的其他分布作为独立同分布，和不同的样本平均（英语：Sample mean and covariance）： ratio=: [: : (-&1@#/.~@(,~ i.)~ % #@]) 10&histogram @ ((#wt)&ratio) @ (wt&ran) 1e6 random=: [: : (#@[ %~ (?@$&0@] + ((I.~ (+/\ % +/))~ ?@$&0))) 20&lineChart @ (80&summary) @ (wt&random) 1e6 20&lineChart @ (80&summary) @ (2 sampleMean (wt&random)) 1e6 20&lineChart @ (80&summary) @ (4 sampleMean (wt&random)) 1e6 操纵SQLite数据库的需要安装相应插件： load 'pacman' NB. 加载包管理器 'install' jpkg 'data/sqlite' NB. 安装SQLite数据库插件 load 'data/sqlite' NB. 加载SQLite数据库插件 getbin_psqlite_ '' NB. 安装SQLite数据库的共享库 SQLite数据库的简单用例： load 'data/sqlite' NB. 加载SQLite数据库插件 db=: sqlopen_psqlite_ '~addons/data/sqlite/db/sandp.db' NB. 打开样例数据库文件 sqltables__db '' NB. 查看所有表格名字 ┌─┬─┬──┐ │p│s│sp│ └─┴─┴──┘ sqlmeta__db 's' NB. 查看表格s的结构 ┌───┬──────┬────┬───────┬──────────┬──┐ │cid│name │type│notnull│dflt_value│pk│ ├───┼──────┼────┼───────┼──────────┼──┤ │0 │sid │text│0 │NULL │1 │ │1 │name │text│0 │NULL │0 │ │2 │status│int │0 │NULL │0 │ │3 │city │text│0 │NULL │0 │ └───┴──────┴────┴───────┴──────────┴──┘ ds=: sqlread__db 'select * from s' NB. 读取表格s，结果表格形状扁长不适合直接展示 dict=: |: @:> NB. 以字典方式显示表格数据 ] rs=: dict ds ┌──────┬──────────────────────────────────┐ │sid │┌──┬──┬──┬──┬──┐ │ │ ││s1│s2│s3│s4│s5│ │ │ │└──┴──┴──┴──┴──┘ │ ├──────┼──────────────────────────────────┤ │name │┌─────┬─────┬─────┬─────┬─────┐ │ │ ││smith│jones│blake│clark│adams│ │ │ │└─────┴─────┴─────┴─────┴─────┘ │ ├──────┼──────────────────────────────────┤ │status│20 10 30 20 30 │ ├──────┼──────────────────────────────────┤ │city │┌──────┬─────┬─────┬──────┬──────┐│ │ ││london│paris│paris│london│athens││ │ │└──────┴─────┴─────┴──────┴──────┘│ └──────┴──────────────────────────────────┘ rs -: sqldict__db 's' 1 cols=: {: @:> NB. 表格数据的诸列列表 cs=: cols ds cs -: sqlexec__db 's' 1 ('s_'&, &.> @ {. @:> ds) =: cs NB. 将表格的诸列并行赋值给添加了表名前缀的诸列名 s_status 20 10 30 20 30 s_sid ┌──┬──┬──┬──┬──┐ │s1│s2│s3│s4│s5│ └──┴──┴──┴──┴──┘ ({. @:> ds) -: sqlcols__db 's' 1 reads=: ({. , (,@> &.>)@}.) @:> NB. 格式化显示表格数据 ] rs=: reads ds ┌───┬─────┬──────┬──────┐ │sid│name │status│city │ ├───┼─────┼──────┼──────┤ │s1 │smith│20 │london│ │s2 │jones│10 │paris │ │s3 │blake│30 │paris │ │s4 │clark│20 │london│ │s5 │adams│30 │athens│ └───┴─────┴──────┴──────┘ $ @ (2&{:: @ {:) rs NB. 第3列数据的形状 5 1 rs -: sqlreads__db 's' 1 readm=: ({. ; <@|:@:(< @ > @ >)@{:) @:> NB. 以矩阵显示表格数据 ] rs=: readm ds ┌──────────────────────┬────────────────────┐ │┌───┬────┬──────┬────┐│┌──┬─────┬──┬──────┐│ ││sid│name│status│city│││s1│smith│20│london││ │└───┴────┴──────┴────┘│├──┼─────┼──┼──────┤│ │ ││s2│jones│10│paris ││ │ │├──┼─────┼──┼──────┤│ │ ││s3│blake│30│paris ││ │ │├──┼─────┼──┼──────┤│ │ ││s4│clark│20│london││ │ │├──┼─────┼──┼──────┤│ │ ││s5│adams│30│athens││ │ │└──┴─────┴──┴──────┘│ └──────────────────────┴────────────────────┘ rs -: sqlreadm__db 's' 1 cp=: '~addons/data/sqlite/db/sandp.db' ; '~/test_sandp.db' db=: sqlcopy_psqlite_ cp NB. 复制数据库并打开复本 cls=: sqlcols__db 's' NB. 得到表格s的列名列表 dat=: ('s6';'s7') ; ('brown';'eaton') ; 40 10 ;< 'rome';'madrid' sqlinsert__db 's' ; cls ;< dat NB. 将数据插入表格s 0 3 sqltail__db 's' NB. 返回最后3个格式化记录 ┌───┬─────┬──────┬──────┐ │sid│name │status│city │ ├───┼─────┼──────┼──────┤ │s5 │adams│30 │athens│ │s6 │brown│40 │rome │ │s7 │eaton│10 │madrid│ └───┴─────┴──────┴──────┘ sqlclose__db '' NB. 关闭数据库 1 J语言采用命名语境实现类，采用编号语境实现对象，下面示例建立字典类： cocurrent 'Dict' create=: {{o [ DEFAULT__o=: 0$0 [ o=. conew 'Dict'}} get=: {{". 'ITEM_',y}} set=: {{ ('ITEM_',y)=: DEFAULT : ('ITEM_',y)=: x}} del=: {{erase 'ITEM_',y}} pop=: {{r [ del y [ r=. get y}} default=: {{DEFAULT=: y}} filt=: {~ I.@:({.@('ITEM_'&E.)@>) len=: {{# filt namelist 0}} list=: {{5&}.&.> filt namelist 0}} in=: {{+/@:(-:&y@>) list ''}} clear=: {{#@,@:(erase @>) filt namelist 0}} copy=: {{o=. conew 'Dict' o [ ".@(,&'__o=:',(5!:5)@<)&.> 'DEFAULT';filt namelist 0}} destroy=: codestroy cocurrent 'base' 在家目录中建立一个dict.ijs文件并录入上述代码，接着以如下代码建立字典对象并对其进行检视和简单操作： load '~/dict.ijs' conl 0 NB. 检视命名语境 ┌────┬────┬─┬────────┬──────┬─────┬─┐ │Dict│base│j│jcompare│jregex│jtask│z│ └────┴────┴─┴────────┴──────┴─────┴─┘ namelist_Dict_ 3 NB. 检视Dict类的动词 ┌─────┬────┬──────┬───────┬───┬───────┬────┬───┬──┬───┬────┬───┬───┐ │clear│copy│create│default│del│destroy│filt│get│in│len│list│pop│set│ └─────┴────┴──────┴───────┴───┴───────┴────┴───┴──┴───┴────┴───┴───┘ d=: create_Dict_ '' d NB. 变量保存的是盒装字符串 ┌─┐ │0│ └─┘ namelist__d 0 NB. 检视d对象的名词 ┌─────────┬───────┐ │COCREATOR│DEFAULT│ └─────────┴───────┘ conl 1 NB. 检视编号语境 ┌─┐ │0│ └─┘ copath <'0' NB. 检视编号语境的查找路径 ┌────┬─┐ │Dict│z│ └────┴─┘ set__d 'i1' (2 3) set__d 'i2' 2 3 'abc' set__d 'i3' abc len__d '' 3 list__d '' ┌──┬──┬──┐ │i1│i2│i3│ └──┴──┴──┘ e=: copy__d '' get__d 'i2' 2 3 del__d 'i2' 1 in__d 'i3' 1 pop__d 'i3' abc clear__d '' 1 list__e '' ┌──┬──┬──┐ │i1│i2│i3│ └──┴──┴──┘ 秩 (J编程语言) Roger K.W. Hui, Kenneth E. Iverson, E. E. McDonnell（英语：Eugene McDonnell）, Arthur T. Whitney. APL\?. 1990 [2022-06-12]. （原始内容存档于2022-06-14）. This paper describes a version of APL based upon the dictionary, but significantly simplified and enhanced, and directly usable on any machine that provides ASCII characters. It also describes salient features of a C implementation that has been tested on several machines, and is available as freeware.  https://code.jsoftware.com/wiki/System/ReleaseNotes/J9.5. SuperCollider documentation, Adverbs for Binary Operators. [2020-04-18]. （原始内容存档于2020-12-11）.  Release Notes J9.4. [2023-03-25]. （原始内容存档于2023-05-08）.  K. E. Iverson. A Personal View of APL. 1991 [2022-06-12]. （原始内容存档于2022-06-12）. Roger and I then began a collaboration on the design and implementation of a dialect of APL (later named J by Roger), first deciding to roughly follow “A Dictionary of APL” and to impose no requirement of compatibility with any existing dialect. We were assisted by suggestions from many sources, particularly in the design of the spelling scheme (E.B. Iverson and A.T. Whitney) and in the treatment of cells, items, and formatting (A.T. Whitney, based on his work on SHARP/HP and on the dialect A reported at the APL89 conference in New York).  Roger K. W. Hui, Morten J. Kromberg. APL since 1978. Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020 [2022-06-20]. （原始内容存档于2022-07-10）. In 1989, Iverson, together with Roger Hui and with input from Arthur Whitney, produced J, with a goal of providing a “shareware” APL implementation for use in teaching. The special APL characters were abandoned because it was felt that they require technical solutions which at that time were still prohibitively expensive in an educational environment. …… J was clearly a “rationalization” of SHARP APL. …… ACM SIGAPL, the ACM Special Interest Group on APL, reinterpreted the “APL” in its name in early 2009 as Array Programming Languages, so that J, k, Nial, etc. would be included in its purview.  Kenneth E. Iverson. A Dictionary of APL. 1987 [2022-06-08]. （原始内容存档于2022-06-12）. A dictionary should not be read as an introduction to a language, but should rather be consulted in conjunction with other material that uses the language in some context of interest to the reader. Even the general section on grammar, which may be intelligible even to the beginner, should perhaps be studied only after a certain amount of other exposure to the language. On the other hand, a dictionary should not be used only to find the meanings of individual words, but should also be studied to gain an overall view of the language. In particular, the grammar may be profitably reviewed again and again in the light of increased knowledge of the language, and the study of groups of related verbs and adverbs can reveal important relationships otherwise easily overlooked.  Kenneth E. Iverson. Operators and Functions. 1978 [2022-06-21]. （原始内容存档于2022-06-24）. Nuclear Axis Operators － The nuax operator (denoted by ⍤) applies to a function left argument and a variable right argument to specify the axes which define the nuclei to which the function is to apply. ……The coax operator ⍥ is also provided; its argument specifies the axes complementary to the nuclear axes.  Kenneth E. Iverson. Rationalized APL. 1983 [2022-06-19]. （原始内容存档于2022-07-22）. In conventional APL, the scalar functions (which apply to scalar elements and produce scalar results) extend to higher rank arrays according to simple general rules; no corresponding general rules exist for the remaining so-called mixed functions. …… Function rank is the most important notion needed to provide a simple and systematic basis for the uniform treatment of all “mixed” or non-scalar functions. …… If f has rank r, then f⍵ is determined by applying f to each of the “cells” of shape (-r)↑⍴⍵, producing a common shape s for each, and assembling the whole into a result of shape ((-r)↓⍴⍵),s. …… If the function g←f⍤r is to be applied dyadically as well as monadically (the only cases addressed in the preceding sections), then it is necessary that r specify three independent ranks, the monadic, the left, and the right. The general argument r is therefore a three-element vector that specifies the ranks in the order just indicated. Moreover, r is extended by reshape if necessary, so that f⍤r ←→ f⍤(⌽3⍴⌽r).  Kenneth E. Iverson, Eugene McDonnell（英语：Eugene McDonnell）. Phrasal Forms. APL 89 Conference Proceedings. August 1989 [2022-06-09]. （原始内容存档于2022-06-12）. In combinatory logic one of the most useful primitive combinators is designated by S [Sch24]. Curry defines Sfgx in prefix notation to be fx(gx) [CuFeCr74]. In common mathematical infix notation this would be given by (x)f(g(x)), which one can write in APL as xfgx, and this is the hook form (fg)x. The combinatory logician appreciates this form because of its great expressiveness: it can be shown that S, along with K, the constancy combinator, suffice to define all other combinators of interest [Ro50]. (The constancy combinator K is defined in infix notation so that cKx has the value c for all x.) Users of APL will appreciate the hook for the same reasons.  Kenneth E. Iverson, Eugene McDonnell（英语：Eugene McDonnell）. Phrasal Forms. APL 89 Conference Proceedings. August 1989 [2022-06-09]. （原始内容存档于2022-06-12）. Curry [Cu31] defines a formalizing combinator, Φ, in prefix notation, such that Φfghx means f(gx)(hx). In common mathematical infix notation this would be designated by (g(x))f(h(x)). An example of this form is Φ+sin2cos2θ, meaning sin2θ+cos2θ. The fork (f g h)⍵ has the same meaning, namely (f⍵)g(h⍵). Curry named this the formalizing combinator because of its role in defining formal implication in terms of ordinary implication. Iverson and Whitney have made several earlier suggestions of ways to achieve what the fork form provides: the scalar operators of [Iv78], [Iv79a], [Iv 79b], the til operator of [Iv82], the union and intersection conjunctions of [Iv87], and the yoke adverb of [Iv88]. Benkard [Bk87] has also suggested a way to achieve the meaning of this form, in his proposal for ↑g/(f h)⍺ ⍵, using the notion of function pair (↑ is APL2’s first function). The present proposal has significant advantages over these earlier ones.  Roger K.W. Hui, Kenneth E. Iverson, Eugene E. McDonnell（英语：Eugene McDonnell）. Tacit Definition. 1991 [2022-06-11]. （原始内容存档于2022-07-06）. To appreciate the more general use of tacit definition, it is necessary to understand three key notions of J: cells and rank, forks, and composition.…… The conjunction & is called with, and applies to nouns (variables) a and b as well as to verbs f and g as follows:   a&g y is a g y   f&b y is x f y   f&g y is f g y x f&g y is (g x) f (g y) …… A number of other constructs in J similarly enhance the utility of tacit definitions. The more important are the under (or dual), atop (a second form of composition), the power conjunction ^:, and further forms of partitions.  Jsoftware. Changes in Version 3.2, 1991 06 02. 1991 [2022-11-07]. （原始内容存档于2022-06-14）. @.    agenda @:    at &:    appose  Robert Bernecky（英语：Robert Bernecky）, Roger K. W. Hui. Gerunds and representations. 1991 [2022-06-12]. （原始内容存档于2022-06-12）. Gerunds, verbal forms that can be used as nouns, are recognized as having utility in the realm of programming languages. We show that gerunds can be viewed as arrays of atomic repmentations of verbs (functions), in a way which is consistent with the syntax and semantics of APL, and which allows verbs to be first class objects in the language. We define derivations of verbs from gerunds in the J dialect of APL, and show how these derivations provide control structures for sequencing, selection (in the sense of generalized forms of CASE or SWITCH statements and IF/THEN/ELSE), iteration (DO UNTIL), recursion, and parallel computation (MIMD, or Multiple Instruction, Multiple Data). We conclude with alternative representations of verbs which are useful in other contexts.  Jsoftware. Changes in Version 4.0, 1991 11 23. 1991 [2022-06-12]. （原始内容存档于2022-06-14）. `:1    replaced by u^:v `:4    replaced by m~ `:5    replaced by @.  Roger K. W. Hui, Morten J. Kromberg. APL since 1978. Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020 [2022-06-20]. （原始内容存档于2022-07-10）. For years, Iverson struggled to achieve in APL the effect of f+g and f×g as they are written in calculus. …… Finally, trains AKA forks were invented [Iverson and McDonnell 1989]. …… Moreover, (f … h p q r) ↔ (f … h (p q r)), and an isolated sequence of two functions is also assigned a meaning (atop, described below), so that a train of any length, even or odd, is interpreted. …… Subsequently, it was realized that trains greatly increase the possibilities for “tacit definition”, expressions consisting of compositions of functions which do not explicitly mention the arguments [Hui et al. 1991]. Trains are implemented in several dialects: J [Hui et al. 1991], NARS2000 [Smith 2020], NGN APL [Nickolov 2013], and Dyalog APL [Scholes 2013]. …… The expressive completeness of trains depends on an atop composition of two functions …… Dyalog APL defines 2-trains as atop. That, together with the functions ⊣(left) and ⊢(right), allows many common compositions to be written as trains.  Roger Hui earns the Iverson Award. [2022-07-30]. （原始内容存档于2022-07-30）.  Vocabulary/Words. [2020-05-18]. （原始内容存档于2016-03-07）.  Kenneth E. Iverson. Rationalized APL. 1983. The enclose function as defined in [Operators and Enclosed Arrays] has made it possible to produce by straightforward APL functions the “index lists” required in indexing expressions of the form a[i;j], and therefore makes it possible to define a corresponding indexing function, which will be denoted by { and called from:     i{a ←→ a[>i[0];>i[1]; ...] Since the disclose function > is permissive, the selection of any single element of a can be written without enclosures as, for example, 1 2 3{a3. Moreover, the left rank of { is 1 and its right rank is infinite, so that …… a simple left argument i of rank greater than 1 produces an array of shape ¯1↓⍴i of elements chosen by the index vectors along its last axis, yielding what is sometimes called “scattered” indexing. For examp1e:     (3 2⍴⍳6){a2 ←→ a2[0;1],a2[2;3],a2[4;5] …… In forming the left arguments of the indexing function, it will often be convenient to use the link function ⊃ defined as follows:      ⊃b ←→ <b if b is simple             b if b is non-simple     a⊃b ←→ (<a),⊃b For example, (2 3⊃4⊃∘⊃5 6){a4 ←→ a[2 3;4;;5 6]. The indexing function { as defined thus far provides all of the facilities provided by conventional indexing, and “scattered” and “complementary” indexing as well. Its power is further enhanced by allowing negative indexing …….  Roger Hui. Some Uses of { and }. 1987. Dyadic { encompasses all computations expressible by [;] indexing of APL\360, as well as the new negative indexing and complementary indexing.  IBM. APL Language (PDF). June 1976 [2022-07-02]. （原始内容存档 (PDF)于2019-09-26）. For vectors R and X, the decode(or base-value) function R⊥X yields the value of the vector X evaluated in number system with radices R[1],R[2],...,R[⍴R]. …… Scalar(or one-element vector) arguments are extended to conform, as required. …… the decode function is extended to arrays in the manner of the inner product: each of the radix vectors along the last axis of the first argument is applied to each of the vectors along the first axis of the second argument.  J语言的#.并未继承IBM对APL解码函数⊥的扩展规定，它可以实现为： decode=: {{x #."(1 _) (0|: y)}} Kenneth E. Iverson. Operators and Functions. 1978 [2022-06-21]. （原始内容存档于2022-06-24）. We also introduce a form of indexing called from denoted by ⌷, ……. The basic definition is:       i⌷a ↔ (,a)[⍉(⍴a)⊥⍉i] The function ⌷ distributes over any scalar function; thus, i⌷a+b ↔ (i⌷a)+(i⌷b). …… For example:       m←3 4⍴⍳12       m 0 1  2  3 4 5  6  7 8 9 10 11       2 2 ⌷ m 10 ……       (3 2⍴3|⍳6)⌷m 1 8 6  J语言在定义上述的⌷（from）之时，解码前后不需要专门进行一元转置⍉运算： from=: {{(($ y) #. x) {::"(0 _) (, y)}} ]m=: 3 4$i.12 0 1 2 3 4 5 6 7 8 9 10 11 2 2 from m 10 3 2$3|i.6 0 1 2 0 1 2 (3 2$3|i.6)from m 1 8 6 IBM. APL Language (PDF). June 1976 [2022-07-02]. （原始内容存档 (PDF)于2019-09-26）. APL functions apply to collections of individual items called arrays. Arrays range from scalars, which are dimensionless, to multi-dimensional arrays of arbitrary rank and size.  Bob Smith. Nested arrays, operators, and functions. 1981 [2022-06-19]. （原始内容存档于2022-06-20）. The data structures of APL are rectangular, multi-dimensional, and flat -- the latter term meaning that all items of arrays are simple scalars. …… In general, a system with nested arrays extends the power of APL by being able to represent data of non-zero depth. Also, in keeping with the past, the system includes a set of primitive functions and operators, tailor-made for manipulating these new data structures. Continuing the above example, the names of the months of the year are best represented in a 12-item vector whose items are each character vectors. This structure has a depth of one. Note that because the individual names are in separate items, we need no longer resort to artifices like pad characters. Moreover, explicit delimiters are not needed as the separation between items is represented through structure rather than data. This particular representation is called a vector of vectors, and can be created as follows:     MONTHS ← ('JANUARY') ('FEBRUARY') ... The above line also illustrates strand notation, used to enter a nested vector in a simple and convenient manner. …… Of the several new operators, the only one specific to nested arrays is the each operator(symbol ¨), which is monadic as an operator, and produces an ambivalent derived function. It is used to apply the f unction which is its argument to the items of an array to produce corresponding items in the result. For example, to determine the length of the names of the months in the above example, use     ⍴¨MONTHS (7) (8) (5) (5) (3) (4) (4) (6) (9) (7) (8) (8) Since monadic rho returns a vector , each item of the above result is a vector (specifically in these cases a one-item vector). The parentheses in the display indicate that the item is not a simple scalar.  James A. Brown（英语：Jim Brown (computer scientist)）. The Principles of APL2. TR 03.247. IBM Santa Teresa Laboratory, San Jose, California. 1984 [2022-06-23]. （原始内容存档于2022-06-12）. APL2 is based on this writer' s PhD Thesis [Br1], the array theory of Trenchard More [Mo1] and most of all on APL1. …… The arrays of APL2 are finite rectangular arrays which contain arrays as items. When the term array is used, it means this subset of all possible arrays. The arrays of APL2 are the same as the arrays of Array Theory and in particular empty arrays have structure as defined by Array Theory [Mo1 etc.]. An array one of whose items is other than a single number or character (a simple scalar) is called a nested array. An array containing only numbers or containing only characters is called a homogeneous array. An array all of whose items are either single numbers or single characters is called a simple array. The arrays of APL1 are simple and homogeneous. In some sense every array in APL2 is nested because it contains other arrays. The term is reserved for those which contain at least one item which is not a single number or character. Thus the universe of arrays is partitioned into two subsets: simple arrays and nested arrays. …… A function is pervasive if pick distributes over it. …… Since the pick function may select an item at an arbitrary depth in a nested array, it may select deep enough to access a simple scalar (because nested arrays have finite depth). Thus a pervasive function may be thought of as applying independently to each simple scalar in its argument(s). …… In APL2 the scalar functions and only the scalar functions are pervasive.  Kenneth E. Iverson. Operators and Functions. 1978 [2022-06-21]. （原始内容存档于2022-06-24）. The enclose function (denoted by <) produces a scalar representation of its argument in the sense that the result is of rank zero, and that there exists an inverse function (called disclose, and denoted by >) such that a ↔ ><a for all a. Any result producible by an expression which does not employ the enclose function is called a simple array, or is said to be simple. Selection and reshaping functions apply without change to non-simple arrays. However, non-simple arrays are outside the domain of all other functions except for enclose, disclose, and equality (together with those functions such as ≠ and ∊ which are defined in terms of equality). The equality function is extended to non-simple scalar arguments as follows:     1. (<a)≠a for all a     2. If a equals b(in rank, shape, and all elements), then (<a)=(<b) yields 1 …… The disclose function is scalar in the sense that it applies to each element of its argument, the new axes disclosed becoming the final axes of the result. …… The disclose function applied to a simple array a produces a result identical to a. Thus (<a)=<>a is a test for whether a is simple.  Kenneth E. Iverson. Rationalized APL. 1983. APL2 provides two significant facilities which apply at “depth” in the enclosure structure of an argument, the dyadic pick function, and the pervasive functions. RS provides no primitive corresponding to pick; it could be defined recursively by:     pick←''∇('→2+0=⍴⍺'⊃'(>0{⍺){(1↓⍺)∆⍵'⊃'⍵') …… Since pervasiveness is a property assigned to functions, it would, in the framework of RS, be provided by an operator. Such an operator could be applied to any function (defined or derived as well as primitive) and, if defined to be dyadic, could provide greater variety. …… If each essential space in an expression is counted as a character, then the link function and strand notation used to form non-simple vectors from simple vectors require expressions of nearly identical length. …… RS does not include the heterogeneous arrays of APL2, and the production of equivalent constructs requires greater use of enclosure. However, the structure of RS does not preclude their introduction. …… The monadic enclose functions defined in RS (<) and in APL2 (⊂) differ in one respect: if s is a simple scalar, then s≡⊂s, but ~s≡<s. Although < can therefore produce some structures not producible by ⊂, the differences between them (in the contexts of the respective systems) cannot, in most cases, be discerned.  Guides/AVX. [2021-12-20]. （原始内容存档于2021-12-20）.  Roger K. W. Hui, Morten J. Kromberg. APL since 1978. Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020 [2022-06-20]. （原始内容存档于2022-07-10）. In order to provide users with access to APIs and frameworks, APL language designers searched for ways to integrate into APL, where everything is an array, selected aspects of the OO paradigm, where everything is an object. …… Some interpreters, like APL+Win, avoided incorporating objects into the APL heap (or “workspace”). …… Other systems added features which allowed variables, functions and operators to be organized in dynamic objects in the workspace. The resulting containers are known as namespaces (Dyalog APL) and locales (J). k implements dictionaries, which are also containers for arrays, but are typically used to contain arrays representing the columns of a relational table. …… The same language features which supported namespaces or classes within APL were used to wrap the external objects used by object oriented APIs, or platforms like the Microsoft’s OLE and .NET frameworks. The choice of syntax for referring to a member name of an object emp was not straightforward. …… Most APL systems did adopt the notation from other OO languages — with a few exceptions.   emp.name    in most APL systems   name__emp   in J (retaining right-to-left evaluation)   emp[`name]  in k (using a symbol within index brackets to select from a dictionary) …… Although APL interpreters have had extensive support for object oriented programming for nearly two decades, most APL users still feel that object and array paradigms are an awkward fit. …… Many of the benefits of OO are related to taking advantage of types, while much of the strength of the APL family is that you can write code which is shape, rank, and type agnostic — achieving many of the same goals as OO through radically different mechanisms.  语境. [2022-09-29]. （原始内容存档于2022-09-29）.  Chris Burke. J4 and OOP. 1998 [2022-11-08]. （原始内容存档于2022-11-08）.  Roger Stokes. Chapter 25: Object-Oriented Programming. 2015 [2020-05-18]. （原始内容存档于2020-06-25）.  J9 base library. [2022-10-20]. （原始内容存档于2022-12-19）.  标准库 （页面存档备份，存于互联网档案馆） 包管理器. [2020-05-22]. （原始内容存档于2021-04-06）.  插件 （页面存档备份，存于互联网档案馆） Run a sentence and produce a 2D display of results. [2022-10-30]. （原始内容存档于2022-10-30）.  Dissect （页面存档备份，存于互联网档案馆） Jdatabase (Jd) is a relational database management system (RDBMS) from Jsoftware that is implemented in J. [2022-09-25]. （原始内容存档于2022-12-06）.  Bussell, Brian; Taylor, Stephen, Software Development as a Collaborative Writing Project, Extreme programming and agile processes in software engineering, Oulu, Finland: Springer: 21–31, 2006, ISBN 978-3-540-35094-1, In practice, competence in the business and in writing software coincide only where the business requires mathematical skills. In consequence, highly abstract executable notations such as APL, A+, J, K, Q, R and S flourish primarily among actuaries, financial traders and statisticians. …… A key technique to the success of this has been programmers and users collaborating on writing the source code. …… Programmers have made this possible by constructing local, domain-specific executable notations. The vocabulary of these notations is drawn from the users’ talk about the work. …… Users and programmers can now converge quickly on and verify a common understanding. The notation enables them to avoid ambiguity; it is a “tool for thought” in the sense of Iverson’s Turing Award lecture [11]. Because the notation is executable (and interpreted), the running system animates the described behaviour in front of them.  Holt, Alan, Network Performance Analysis: Using the J Programming Language, Springer, 2007, ISBN 978-1-84628-822-7  Eric Iverson. J Source GPL. J programming mailing list. 1 March 2011 [2020-05-18]. （原始内容存档于2016-09-23）.  Jsoftware's sourcing policy. [2020-05-18]. （原始内容存档于2021-01-26）.  NuVoc （页面存档备份，存于互联网档案馆） 词类（页面存档备份，存于互联网档案馆） 名词（页面存档备份，存于互联网档案馆） 动词（页面存档备份，存于互联网档案馆） 定语 （页面存档备份，存于互联网档案馆） IBM. APL Language (PDF). June 1976 [2022-07-02]. （原始内容存档 (PDF)于2019-09-26）. For example, the grade function ⍋ is commonly used to produce indices needed to reorder a vector into ascending order (as in X[⍋X]), but may also be used in the treatment of permutations as the inverse function, that is, ⍋P yields the permutations as the inverse to P.  Read and write CSV files and strings. [2022-06-14]. （原始内容存档于2022-07-07）.  Addons/tables/csv （页面存档备份，存于互联网档案馆） Roger K.W. Hui. Extended Integers in J. 1996 [2022-06-24]. （原始内容存档于2022-06-28）. Some verbs v signal domain error on some extended arguments because the result is not integral; however, <.@v and >.@v are closed on extended arguments.  J Memory Mapped File. [2022-10-20]. （原始内容存档于2022-10-20）.  内存映射文件 （页面存档备份，存于互联网档案馆） K.E. Iverson. Determinant-Like Functions Produced by the Dot Operator. 1982. The operator denoted by the dot has been extended to provide a monadic function (as in -.×m) as well as the established dyadic inner product function (as in n+.×m). …… The determinant of a square matrix m is defined as the alternating sum (i.e. reduction by -) of the !n←1↑⍴m products over n elements chosen (in each of the !n possible ways) one from each row and column. Analogous calculations in which other function pairs are substituted for - and × lead to other useful functions; examples include the pairs ⌈⌊, ∧∨, and +×, the last (called the permanent) being useful in combinatorics.  IBM. APL Language (PDF). June 1976 [2022-07-02]. （原始内容存档 (PDF)于2019-09-26）. If P and Q are vectors of the same shape, then the expression +/P×Q has a variety of useful interpretations. …… The inner product produces functions equivalent to expressions in this form; it is denoted by a dot and applies the two function surround it. Thus P+.×Q is equivalent to +/P×Q, and P×.⋆Q is equivalent to ×/P⋆Q, and, in general, Pf.gQ is equivalent to f/PgQ, if P and Q are vectors. The inner product is extended to arrays other than vectors along certain fixed axes, namely the last axis of the first argument and the first axis of the last argument. the lengths of these axes must agree. the shape of the result is obtained by deleting these axes and chaining the remaining shape vectors together. …… The inner product M+.×N is commonly called the matrix product. …… Either argument of the inner product may be a scalar or a one-element vectors; it is extended in the usual manner.  IBM. APL Language (PDF). June 1976 [2022-07-02]. （原始内容存档 (PDF)于2019-09-26）. The outer product operator, denoted by by the symbols ∘. preceding the function symbol, applies to any dyadic primitive scalar function, so that the function is evaluated for each member of the left argument paired with each member of the right argument. …… Such tables may be better understood if labelled in a way widely used in elementary arithmetic texts: values of the arguments are placed beside and above the table, and the function whose outer product is being computed is shown at the corner.  Kenneth E. Iverson. A Dictionary of APL. 1987. The case 3⍤v also has left rank 2 , and ⍺3⍤v⍵ applies v to each element produced by a tessellation of ⍵, using a size 1{⍺, and beginning points that are multiples of the “shift” 0{⍺.  Vocabulary/SpecialCombinations. [2023-05-08]. （原始内容存档于2023-05-08）.  秩 （页面存档备份，存于互联网档案馆） Roger K. W. Hui, Morten J. Kromberg. APL since 1978. Proceedings of the ACM on Programming Languages, Volume 4, Issue HOPL. 2020 [2022-06-20]. （原始内容存档于2022-07-10）. In the dyadic case, two frames lf and rf are involved, from the left and right arguments. Several different treatments are possible: • scalar agreement: (lf≡rf)∨(lf≡⍬)∨(rf≡⍬), the left and right frames match, or one is the empty vector (⍬). If the frames match, there are an equal number of left and right cells, and the operand function applies to corresponding cells. If they do not match, one frame must be ⍬, that is, there is one cell on one side, whence that one cell is applied against every cell on the other side. Scalar agreement is implemented in Dyalog APL [Dyalog 2015]. • prefix agreement: (p↑lf)≡(p↑rf) ⊣ p←(≢lf)⌊(≢rf), one frame must be a prefix of the other. Let ff be the longer frame (that is, ff←lf,p↓rf). In this case a cell of the argument with the shorter frame is applied against ×⌿p↓ff cells of the other argument. Prefix agreement is implemented in J [Hui and Iverson 2004], and is consistent with the emphasis on the leading axis (§2). • suffix agreement: one frame must be a suffix of the other. J had suffix agreement before it switched to prefix agreement in 1992 on a suggestion by Whitney [Whitney 1992]. • strict agreement: the frames must match. No dialect has ever implemented this.  动词信息. [2022-06-04]. （原始内容存档于2022-07-06）.  框架填充. [2022-06-07]. （原始内容存档于2022-07-06）.  定语生成的动词的秩. [2022-06-04]. （原始内容存档于2022-07-07）.  Kenneth E. Iverson. Operators and Functions. 1978 [2022-06-21]. （原始内容存档于2022-06-24）. The dual operator, denoted by ⍢, is a slight extension of the notion of dual functions implicit in deMorgan’s law (∨⍢~ ↔ ^ and ≠⍢~ ↔ =), the extension being to include a monadic left argument, as in ⌊⍢-x ↔ ⌈x. …… Composition and the dual operator applied to a divalent left argument and a monadic (or divalent) right argument yield parallel definitions of divalent derived functions as follows: …… Dual:  f⍢g y ↔ (g⍣¯1) f (g y)     x f⍢g y ↔ (g⍣¯1) (g x) f (g y) It should be noted that the extension of the dual to include the monadic definition makes the identities ⌈⍢- ↔ ⌊ and ⌊⍢- ↔ ⌈ hold for both the monadic case (floor and ceiling) and for the dyadic case (minimum and maximum). Moreover, for the dyadic case the exponential function yields the identities ×⍢* ↔ + and +⍢⍟ ↔ ×, the latter of which provides the basis for the use of natural logarithms in multiplication, just as the identity +⍢(10¨⍟) ↔ x forms the basis for the use of base ten logarithms.  Kenneth E. Iverson. Operators and Functions. 1978 [2022-06-21]. （原始内容存档于2022-06-24）. The expression f⍢> produces a derived function which applies the function f to its argument in an “item-wise” fashion, by disclosing each element of the argument, applying f, and enclosing the result to produce the corresponding element of the overall result.  Roger Hui. Remembering Ken Iverson. 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.  Function composition. [2022-06-09]. （原始内容存档于2022-07-06）.  Kenneth E. Iverson. A Dictionary of APL. 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. A Dictionary of APL. 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. A Dictionary of APL. 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. A Dictionary of APL. 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. APL since 1978. 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. Operators and Functions. 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.  Vocabulary/Inverses. [2022-10-23]. （原始内容存档于2022-12-05）.  Kenneth E. Iverson, Peter K. Wooster. A Function Definition Operator. 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. Rationalized APL. 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（英语：John M. Scholes）. Direct Functions in Dyalog APL (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.  System/ReleaseNotes/J902. 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.  DGEMM - matrix matrix multiply. [2022-10-13]. （原始内容存档于2022-10-10）.  控制结构 A. D. Falkoff（英语：Adin Falkoff）, K. E. Iverson. APL\360 User's Manual (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. …… Roger Hui. dyadic transpose, a personal history. 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. APL Language (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. Operators and Functions. 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|:a ↔ (<(/:j){k){a，而恒等式i|:j|:a ↔ (i{j)|:a的推导过程相应如下： (<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. APL since 1978. 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. APL since 1978. 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. APL Language (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. APL since 1978. 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. APL since 1978. 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. APL since 1978. 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 ∘.=⍨ ⍳⍵}  A History of APL in 50 Functions － 19. Permutations. [2022-09-27]. （原始内容存档于2022-09-27）.  J implementation of Fold primitives. [2022-11-01]. （原始内容存档于2022-11-01）.  Roger K. W. Hui, Morten J. Kromberg. APL since 1978. 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.  Stencil Lives. 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. APL since 1978. 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. APL since 1978. 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. APL since 1978. 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.  Sqlite enhanced API for J. [2022-10-20]. （原始内容存档于2022-10-22）.  Addons/data/sqlite/Overview. [2022-09-20]. （原始内容存档于2022-09-20）.  JSoftware（页面存档备份，存于互联网档案馆），J的官方網站 GitHub上的Jsoftware頁面 – Repository Learning J （页面存档备份，存于互联网档案馆） – An Introduction to the J Programming Language by Roger Stokes J for C Programmers （页面存档备份，存于互联网档案馆） by Henry Rich J for the APL Programmer （页面存档备份，存于互联网档案馆） by Chris Burke and Roger Hui An Implementation of J （页面存档备份，存于互联网档案馆） by Roger K.W. Hui APL to J Phrasebook （页面存档备份，存于互联网档案馆） J － APL Wiki （页面存档备份，存于互联网档案馆） 郭平欣譯J字典 （页面存档备份，存于互联网档案馆）