14. 分區
函數 sum=: +/ 作用於其引數的各子項原子;要做部分子項的小計,有必要將之作用於引數的所有字首。例如:
sum=: +/ [. a=: 1 2 3 4 5 6
(sum a) ; (sum\ a)
+--+--------------+
|21|1 3 6 10 15 21|
+--+--------------+
單字 \ 代表字首副詞,將其引數(前例為 sum)作用於其動詞引數(前例之1
2 3 4 5 6)的所有字首(前例中,1, 1 2, 1 2 3,..., )。副詞 \. 功能相同,唯作用於字尾:
sum\. a
21 20 18 15 11 6
單邊字 < 將其引數封裝,而動詞 <\ 與 <\.
則顯示分區的效果。例如:
<1 2 3
+-----+
|1 2 3|
+-----+
(<1),(<1 2),(<1 2 3)
+-+---+-----+
|1|1 2|1 2 3|
+-+---+-----+
<\ a
+-+---+-----+-------+---------+-----------+
|1|1 2|1 2 3|1 2 3 4|1 2 3 4 5|1 2 3 4 5 6|
+-+---+-----+-------+---------+-----------+
<\. a
+-----------+---------+-------+-----+---+-+
|1 2 3 4 5 6|2 3 4 5 6|3 4 5 6|4 5 6|5 6|6|
+-----------+---------+-------+-----+---+-+
斜切副詞 /. 沿著順對角線分區表格。因此:
</. t=: 1 2 1 */ 1 3 3 1
+-+---+-----+-----+---+-+
|1|3 2|3 6 1|1 6 3|2 3|1|
+-+---+-----+-----+---+-+
t ; (sum/. t) ; (10 #. sum/. t) ; (121*1331)
+-------+-------------+------+------+
|1 3 3 1|1 5 10 10 5 1|161051|161051|
|2 6 6 2| | | |
|1 3 3 1| | | |
+-------+-------------+------+------+
練習
14.1 |
Define programs analogous to sum=:+/\ for
progressive products, progressive maxima,與progressive minima.
|
14.1 |
Treat following programs與comments like those of
Section 12, 亦即,as exercises in reading與writing.
Experiment with expressions such as c pol x and c pp d
and (c pp d) pol x with c=:1 3 3 1
and d=:1 2 1 and x=:i.5 .
See dictionary or Section 20
for use of rank:
pol=: +/@([*]^i.@#@[)"1 0 | 多項式 |
pp=: +//.@(*/) | 多項式 product |
pp11=: 1 1&pp | 多項式 product by 1 1 |
pp11 d |
pp11^:5 (1) |
ps=: +/@,: | 多項式 sum |
|
下個 前個
字彙
索引
主選單