SPL Programming - 4.3 [Sequence] Multi-layer sequence
The members of a sequence can also be sequences, and a multi-layer sequence can be formed in this way. For example, [[1,2,3],[2,3,1],[3,1,2]] is a legal sequence, and each member is also a sequence.
Let’s look at how members of a multi-layer sequence can be referenced:
A | |
---|---|
1 | =[[1,2,3],[4,5,6],[7,8,9,10]] |
2 | =A1(2) |
3 | =A1(3)(2) |
4 | >A1(1)(3)=0 |
5 | =A1.len() |
6 | =A1(3).len() |
7 | >A1(2)=0 |
Try to execute the above code and observe the running results to understand the actions of member reference and assignment of multi-layer sequence.
The result of A2 is sequence [4,5,6]; A3 gets the second member of the third member sequence, namely 8; After A4 is executed, the third member of the first member sequence becomes 0, which used to be 3; A5 is the length of the sequence, which is 3, and A6 is the length of the third member sequence, which is 4; After A7 is executed, the second member sequence will be changed to 0, and this member will no longer be a sequence.
Multi layer sequence can also be displayed on the interface. After the above code is executed, click A1 to see its value on the right.
The sequence members will also be displayed as a sequence. At this time, double-click a sequence member (such as the third member pointed by the arrow), the members of this member sequence will be displayed:
The sequence composed of n sequences of length m can be understood as a two-dimensional structure, and each member sequence can be written as a row, and it can be regarded as a table of n rows and m columns. Therefore, some programming languages also call a two-layer sequence as a two-dimensional array, and even have done some special processing, and can support the writing method of x(i,j) to access the member of the i-th row and j-th column. There is no special understanding in SPL, it is simply understood as the j-th member of the i-th member sequence, that is, written as x(i)(j).
Obviously, there can be more of these levels, in terms of array, there can be more dimensions.
We use the two-layer sequence to calculate the Pascal triangle:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
……
Pascal triangle is composed of N rows of numbers. The n-th row has n numbers. The m-th number of the n-th row is the sum of the (m-1)-th and m-th numbers of the (n-1)-th row. The n-th row number of Pascal triangle is exactly the coefficient of term xi in (1+x)n, that is:
(1+x)1= 1+1x
(1+x)2= 1+2x+1x2
(1+x)3= 1+3x+3x2+1x3
(1+x)4= 1+4x+6x2+4x3+1x4
……+
The code is simple:
A | B | C | D | |
1 | 5 | =\[0\]*(A1+1) | >B1(1)=\[1\] | >B1(2)=\[1,1\] |
2 | for 3,A1+1 | >B1(A2)=\[1\]*A2 | ||
3 | for 2,A2-1 | >B1(A2)(B3)=B1(A2-1)(B3-1)+B1(A2-1)(B3) |
Calculate to the (A1+1)-th row, the result is a 2-layer sequence, which is saved in B1.
B1 generates a sequence of length A1+1, which is ready to be filled in. C1 and D1 are filled in the first two rows. The A2 loop is calculated from the third row. B2 generates a sequence of length A2, with members all be 1, for the current row, and then calculates from the second column to the A2-1 column, because the first and A2 columns of this row are all 1, and it is unnecessary to calculate them.
SPL Programming - Preface
SPL Programming - 4.2 [Sequence] Loop of sequence
SPL Programming - 4.4 [Sequence] Understanding objects
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL