SPL Programming - 5.1 [Sequence as a whole] Set operations

 

Now, we have learned to use a sequence, but this sequence looks like what we said before, it is a batch of variables with the same name. When we operate on these sequence members, we still refer to and assign values one by one. In this chapter, we learn how to operate a sequence as a whole.

Sequence is essentially a set, so it is easy to think that it can support set related operations, that is, intersection, union, etc.

SPL provides four set operators:

A B
1 =[1,2,3,4,5,1] =[4,6,4,5,1]
2 =A1|B1 =B1|A1
3 =A1&B1 =B1&A1
4 =A1^B1 =B1^A1
5 =A1\B1 =B1\A1
6 =A1|1 =A1&1
7 =1|B1 =1&B1
8 =A1\1 =B1\1

A1| B1 appends the members of sequence B1 to sequence A1 to form a new sequence and return, which is called concatenation. While &,^,\ are the set union, intersection and difference operations of common significance. We used to represent the integer division operation with , here we use it on the sequence to represent the difference set.

It is important to note that, unlike the mathematical disordered set, the sequence is ordered, which will lead to the difference between A1&B1 and B1&A1, and that the sequence intersection and union operations in SPL do not meet the exchange law. The ordered set also allows duplicate members. When performing intersection, union and difference operations, it will be more complex than the set without duplicate members. Please carefully observe the above operation results to understand the operation rules in this case.

In addition, SPL also supports the operation between a sequence and a single value, which is equivalent to the operation with a one-member sequence. The single value can be placed to the left or right of the operator.

We use the set operation to rewrite the narcissus number program:

A B C D E F
1 =0 =[]
2 for 9 =100*A2 =A2*A2*A2
3 for 0,9 =B2+10*B3 =C2+B3*B3*B3 if C3<D3 break
4 for 0,9 =C3+C4 =D3+C4*C4*C4
5 if D4==E4 >B1|=D4
6 else if D4<E4 break

Concatenate the calculated narcissus number to the current sequence with | operation. You can see that the set operation also supports |= syntax.

Pascal triangle:

A B C
1 5 =[[1],[1,1]]
2 for 3,A1+1 =[]
3 for 2,A2-1 >B2|=[B1(A2-1)(B3-1)+B1(A2-1)(B3)]
4 >B1|=[1|B2|1]

Use inner loop to calculate the middle part of the current row, and then concatenate 1 before and after it respectively, and then concatenate it as a whole to the end of the target 2-layer sequence.

As a kind of data, sequence can also be compared:

A
1 =[1,2,3,4]==[1,2,3,4]
2 =[1,2,3,4]!=[1,3,2,4]
3 =[1,2,3,4]<[1,2,4]
4 =[1,2,3,4]<[1,2,3,4,5]

The results of these judgments are all true. The conventional dictionary sorting method is used in sequence comparison. Each pair of members is compared in order, and the values of the first pair of unequal members is used to draw the comparison result of the sequences; If it is always equal, the longer sequence is considered larger; If all the corresponding members are equal and the length is the same, the two sequences are considered equal. In particular, because of order, the sequences composed of same members are not necessarily equal.

We used [0]*n to generate an all-0 sequence of length n. SPL also provides the to() function to generate a sequence of integers.

A B
1 =to(5) [1,2,3,4,5]
2 =to(2,6) [2,3,4,5,6]
3 =to(5,1) [5,4,3,2,1]
4 =to(6,2) [6,5,4,3,2]

The result of column A is equal to the sequence of constants in column B.

SPL refers to the sequence whose members are all integers as an integer sequence.

Regarding the loop statements we have learned, we can find that for n, for a,b are basically equivalent to for to(n) and for to(a,b). Here we say basically equivalent rather than equivalent, because the loop variable values of the former two at the end of the loop are different from those of the latter two.

The sequence numbers of the sequence members are also integers. We can get the members from a sequence to form another sequence according to the sequence numbers specified by the members of an integer sequence. For sequence A and integer sequence p, in SPL, A(p) can be used to represent [A(p(1)),…,A(p(k))], k is the length of p. A (p) is called the generated sequence of A.

A B
1 =[9,8,7,6,5,4,3,2,1] =A1([5,2,6])
2 =A1([1,2,3,5]) =A1.to(5)
3 =A1(to(3,7)) =A1.to(3,7)
4 =A1(to(5,A1.len())) =A1.to(5,)
5 =A1(A1) =A1(A1|to(9))

B1 will get the 5th, 2nd and 6th members of A1 to form a sequence, i.e. [5,8,4]. For the sequence composed of some continuous members in a sequence, it can be obtained by the to() function of the sequence. A2, A3 and A4 can be simplified into B2,B3,B4. Note the writing methods of B2 and B4, and the comma in B4 cannot be omitted.

A5 and B5 are interesting calculations. Please observe the results of them by yourself.

Usually, the generated sequence is part of the original sequence(which can be called a sub sequence at this time), but an integer sequence may also have duplicate members, so the generated sequence is not always the sub sequence of the original sequence.