# SPL Programming - 4.4 [Sequence] Understanding objects

Let’s take a look at an example code:

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

The value of A1 is assigned to B1, and then B1 is changed in A3. If you look at A1, you will find that it has not changed and still is the starting value. That is, the =A1 in cell B1 copies A1, and is irrelevant to the original A1.

Similarly, the value of A2 is assigned to B2, and then one member of B2 is changed in A4. Now we can see that the member of A2 has been changed! This shows that the assignment of sequence is different from that of integer, and the assignment in B2 does not copy A2. A2 and B2 are actually the same thing.

Unlike the single values of integer and floating-point, the sequence is a complex data type composed of a batch of values. This kind of data will occupy a large space in the computer to store, and is not suitable to be directly stored in the memory space occupied by the variable itself, and needs special space and needs to be managed uniformly.

The computer will give a number to every memory space, so that it can find the space with this number later on. This number is also called the address of the memory space, which is very similar to the address we use in daily life. For the complex data type of sequence, the variable stores not the data itself, but its address.

The address of the memory space is also called pointer in many computer materials.

Back to the above code, the assignment statement of B2 only copies the address of sequence A2, not the value. After execution, A2 and B2 store the address of the same sequence. At this time, if you change the member of B2, that is, if you change the member of that sequence, you will also see the change from A2. Similarly, if you change the member of A2, you can also see the change from B2.

The simple values of integer are completely copied when they are assigned values. If you change one of them, it has nothing to do with the other.

The computer community is also used to call the complex data types such as sequence as objects. We’ll come across more types of objects later.

An object usually has many functions related to it. For the function f with object x as the main parameter, it can still be written in the traditional form of f(x,…), but in order to show that f is strongly related to x, we often write it in the form of x.f(…), and also call f the function or method of object x. For example, the function x.len() that returns the length of sequence x.

A large number of functions in SPL are written in the form of such objects.

In the previous code, we need to know the maximum length of a sequence when we want to use a sequence. We first create a sequence with a certain length and then fill in the members in the execution process of the program. It is not convenient. We can not always know the length of the sequence to be used in the program(for example, the number of Narcissus numbers in the previous example, maybe 100 are not enough). If it is long, it will waste space, and if it is short, it will lead to overflow error.

In fact, for the sequence object, SPL provides ways to change its length, which is, to be exact, to insert and delete members.

A
1 =[1,2,3,4,5,6,7,8,9]
2 >A1.insert(1,0)
3 >A1.delete(5)
4 >A1.insert(0,10)
5 >A1.insert(8,78)

Execute the program and observe the changes in A1.

A.insert(i,x) will insert a member x before the i-th position of sequence A, and the original i-th, (i+1)-th,… members will move backward in turn; If i is 0, x is inserted to the end, that is, appending a member. A.insert(…) will increase the length of the sequence. A.delete(i) will delete the i-th member, and the sequence length will be reduced.

We rewrite the Narcissus number program using insert() function:

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.insert(0,D4)
6 else if D4<E4 break

Create an empty sequence in B1, and then append a member in E5 for each narcissus number found. Finally, B1 is the sequence composed of all Narcissus numbers. The length of the sequence is exactly the number of Narcissus numbers, and there is no waste of empty positions.

Pascal triangle can also be implemented in a similar way:

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

B1 initializes the first two rows. In the loop of each following row, a sequence with only head and tail is generated in B2, added to the end of B1, and then the calculation of B2 is completed by inserting the intermediate value in B2 by the loop.

Pay attention to the action of appending B2 to B1 in cell C2. When adding a sequence member to a two-layer sequence, add a layer of [] to the parameter, that is, the [B2] here. This is because the insert()function actually allows a sequence to be inserted into the original sequence at a time. When x is also a sequence, A.insert(i,x) inserts the members of x before the i-th member of A in turn (if i is 0, append to the end). To insert sequence x as a whole (the case of this example), you need to add a layer of [], as a sequence with a length of 1, otherwise, insert() function will take x apart and insert the members one by one.

We’d like to say a light digression here.

In fact, once the memory space in the computer program is created, its size is fixed and can not be changed. The insert()function can change the length of the sequence, and the action behind it is to recreate a larger memory space, copy the original members of the sequence, and discard the original space (return to the operating system).

It is conceivable that insert function will be a very slow action. Frequent use of insert()function will lead to continuous creation and moving of storage space(of course, the memory space will not be recreated every time, it will be created a little larger at once, and be recreated again if not enough), resulting in low performance of operation. We should get used to avoiding it in code.

This principle is applicable to any programming language.