25. 排列

迴文構詞是排列觀念常見的範例:
   w=: 'STOP'
   3 2 0 1 { w
POST

   2 3 1 0 { w
OPTS

   3 0 2 1 { w
PSOT
上述 { 的左引數本身就是條列 i.4 的排列;排列向量的範例,用來以p&{的形式表達排列函數。

p 為排列向量,片語 p&C.也等於排列 p&{ 其他情況下循環函數 C. 取得 函數 { . 並不相同。只有 C. p y 得到排列 p 的循環表示法。例如:
   ]c=: C. p=: 2 4 0 1 3
+---+-----+
|2 0|4 3 1|
+---+-----+

   c C. 'ABCDE'                   C. c
CEABD                          2 4 0 1 3
Each of boxed 分子 of a cycle specify a list of positions that cycle among mselves; 在example above, 分子 from position 3 moves to position 4 , 分子 1 moves to 3 ,and 分子 4 to 1 .

一個n原子的任何排列,都可以其在所有n原子排列升冪表的位置指標代表,此一指標稱為此排列的迴文指標,函數 A. 為迴文指標函數。
   1 A. 'ABCDE'                   A. 0 1 2 4 3
ABCED                          1

   (i.!3) A. i.3                  (i.!3) A. 'ABC'
0 1 2                          ABC
0 2 1                          ACB
1 0 2                          BAC
1 2 0                          BCA
2 0 1                          CAB
2 1 0                          CBA

練習
25.1   Use following as exercises in reading與writing (try on a=:'abcdef' and b=: i. 6 and c=: i. 6 6):
f=: 1&A.                       Interchange last two items
g=: 3&A.                       Rotate last three items
h=: 5&A.                       Reverse last three items
i=: <:@!@[ A. ]                k i a reverses last k items
25.2   Experiment with following expressions與ors like m to determine 規則for using abbreviated引數 to C. and compare your conclusions with dictionary定義:
2 1 4 C. b=:i.6
(<2 1 4) C. b
(3 1;5 0) C. b
25.3   Make a program ac that produces a 表格of cycle representations of all 排列of order of its引數, as in ac 3 .

解答: ac=: C.@(i.@! A. i.)


下個前個字彙索引主選單