SPL Programming - 4.2 [Sequence] Loop of sequence

 

With loop, we can implement some aggregation operations for a sequence, such as the previous max/min.

A B C
1 =[3,4,12,4,6,9,3,5] =-999999 =999999
2 for A1.len() if B1<A1(A2) >B1=A1(A2)
3 if C1>A1(A2) >C1=A1(A2)

A1.len()in cell A2 will return the length of sequence A1. This function in the form of x.f() will be discussed later, and we just let it be here.

This is a standard algorithm for calculating the maximum amd minimum value. For the maximum value, set the target value to a very small number, and then compare it with the sequence member in turn. If the current member is found to be larger, replace the target value with the current member, and then get the maximum value at the end of the loop. The minimum is the opposite process.

The sum is also simple:

A B
1 =[3,4,12,4,6,9,3,5] =0
2 for A1.len() >B1+=A1(A2)

However, we always need to write an A1.len(), and then use the sequence number A1(A2) when referring to members, which is a bit troublesome. In fact, the sequence itself can also be looped. max/min can be written as follows:

A B C
1 =[3,4,12,4,6,9,3,5] =-999999 =999999
2 for A1 if B1<A2 >B1=A2
3 if C1>A2 >C1=A2

for can be directly followed by a sequence, the number of cycles is the length of the sequence, and the loop variable is each member of the sequence in turn. There is no need to write the length of the sequence, and there is no need to use the sequence number to access members.

It’s also easy to write sum:

A B
1 =[3,4,12,4,6,9,3,5] =0
2 for A1 >B1+=A2

In recent years, programming languages have begun to support this kind of set type loop writing method. The early programming languages did not have a strong set concept, so they could only use the previous sequence number writing method.

However, there is a problem. We don’t know the loop number of the current loop. For example, when we calculate the maximum value, we want to know not only what the maximum value is, but also which member is the largest. It is relatively simple if we use the above-mentioned sequence number writing method:

A B C
1 =[3,4,12,4,6,9,3,5] =-999999 =0
2 for A1.len() if B1<A1(A2) >B1=A1(A2)
3 >C1=A2

When we find a larger member, just write down its sequence number. Finally, C1 will be the sequence number of the maximum value.

SPL has taken this into consideration and provided a method to obtain the current loop number even when the sequence loops:

A B C
1 =[3,4,12,4,6,9,3,5] =-999999 =0
2 for A1 if B1<A2 >B1=A2
3 >C1=#A2

When a sequence loops, there are two loop variables. The loop cell itself is a member of the sequence. After adding #, it means the loop number. Here, it is exactly the expected sequence number. This variable with # is usually called the loop number.

Let’s take another example to judge whether two sequences of the same length are identical, that is, each member of the same sequence number is the same. It can be written using a conventional loop like this:

A B C
1 =[1,2,3,4,5] =[1,2,3,4,6]
2 for A1.len() if A1(A2)!=B1(A2) >output(“No”)
3 break

If an unequal member is found, output no, and then jump out of the loop.

It can also be realized by a sequence loop:

A B C
1 =[1,2,3,4,5] =[1,2,3,4,6]
2 for A1 if A2!=B1(#A2) >output(“No”)
3 break

The code for this comparison can also be written as follows:

A B C
1 =[1,2,3,4,5] =[1,2,3,4,6]
2 for A1.len() if A1(A2)!=B1(A2) break
3 if A2<=A1.len() >output(“No”)

After finding an unequal member, jump out of the loop and judge outside the loop. A3 code means that the loop variable still has a value after the end of the loop, and if the loop ends normally, its loop variable will be filled with the number of cycles plus one, which is equivalent to the value of the loop variable if there is another round of loop.

When using a sequence loop, at the end of the loop, the loop variable will be filled with a null value, and the loop number is not defined any more.


SPL Programming - Preface
SPL Programming - 4.1 [Sequence] Sequence
SPL Programming - 4.3 [Sequence] Multi-layer sequence