 Welcome to this new session, in which we will be looking at a new abstract data type now called sequences. So, far we have only looked at basic natural numbers, but we come across many different types in computer science and sequences is one of the most fundamental data types that occurs in computer science. Informally a sequence is just a collection of values of the same type placed in a particular order and the important point here is that the values occur in a particular order. So, at the lowest level if you look at all data in a computer it is nothing but a sequence of bits. So, you have a sequence of memory locations which store bits. So, all data is finally nothing but a sequence of bits and essentially what we need to do is to be able to represent any kind of data using a sequence of bits. So, sequences play a very fundamental role in computer science and we come across many different types which are essentially sequence of objects of a particular type. So, let us look at a formal definition of sequences and the definition is again very similar to that for numbers. So, here we always assume that all the elements in a sequence are of the same type and we will call such a sequence a sequence of type T. So, if all the elements in the sequence are of type T then we call it a sequence of type T and we get different types of sequences based on what the type T is. So, you can have a sequence of bits where each element is a single bit or you can have a sequence of characters or a sequence of numbers or a sequence of sequence of numbers. So, you can have a sequence each element of which is itself a sequence of numbers. So, this will in fact be like matrices as we will see later. A matrix can be thought of as a sequence of sequence of numbers and there are many operations on sequences which actually do not depend on what the type of the elements in the sequence are. So, these operations only depend on the fact that the elements in the sequence appear in a particular order. It does not really matter what the type of the elements is and we can define these type these operations in a much more general way. So, let us look at a formal definition. So, no matter what the type is we always have an empty sequence denoted by phi which we consider to be a sequence of type T. So, just like 0 was considered to be a natural number here we have an empty sequence phi which is always considered to be a sequence of type T no matter what the type T is. And just like we had a next operation for numbers we have another operation here which we call push. So, if s is some sequence of type T and x is any element of type T or a value of type T then push x s gives us another sequence of type T. So, we think of push as an operation which adds one more element to the sequence. So, if we have a sequences then I can perform a push x s operation on that which adds a new element x to the sequence. And the third property essentially says that this is the only way of getting all possible sequences or if a property holds for the empty sequence and assuming it holds for a sequence s it holds for the sequence push x comma s no matter what value x you push into the sequence then it holds for all sequences of type T. Essentially this says that any sequence of type T is obtained by starting with the empty sequence and pushing elements of type T into the sequence. So, like next push is the only operation for generating sequences of type T. So, let us look at a sequence of bits say 1 1 0 1 0 1. So, this is the value which is obtained by at first push 1 in the empty sequence. So, start with always start with the empty sequence at first push 1 then push a 0 then a 1 again a 0 then a 1 then a 1 and so on. So, this will generate the sequence of bits 1 1 0 1 0 1. Similarly you can define the sequence of character is the value obtained by again pushing f in the empty sequence then e then d then c and so on. The sequence containing only the number 10 it is push 10 comma 5. So, I have an empty sequence I push 10 in it I get a sequence which contains only one element 10. Note that the sequence containing the single element is a different value than the number 10 itself. So, this is of type sequence whereas that is of type number and we distinguish between these type because the operations that you can perform on them are different. So, let us look at some operations on sequences and these are defined similarly as they are done for numbers. We first defined the operation for the empty sequence assuming it is defined for a sequence s define it for the sequence push x comma s whatever be the value of x. So, assuming you know what the value of the operation is for the sequence s you try to define it for the sequence obtained by adding the element x to it or pushing the element x into it and this defines it for all sequences of type t and in many of these definitions the type of the element does not matter it could be a sequence of any type and you push any elements of that type n. So, let us look at a simple function called length on the sequence which measures the number or counts the number of elements in it. So, to define formally length of the empty sequence is defined to be 0 and the length of the sequence obtained by pushing any element x in it is the next number of length of s. So, we apply length of s gives us a number we apply the next operation to that that gives us the length of the sequence obtained by pushing an element x into it. Here is another operation on sequences which is needed frequently concatenation of sequences which just means that if I have a sequence s 1 and s 2 I construct a new sequence consisting of s 1 followed by s 2. So, again we will define this for all values of s 2 and considering different values of s 1. So, if s 1 is empty then concatenate 5, s 2 will always be the sequence s 2 for any sequence s 2. So, concatenating anything to an empty sequence gives that sequence which are concatenating. On the other hand concatenating push x s 1, s 2 is essentially I concatenate s 1 and s 2 first and then push x into it that is adding x to s 1 and then appending s 2 to it is essentially equivalent to concatenating s 1 and s 2 and then pushing x into the resulting sequence. So, this defines the concatenate operation for all sequences s 1 and s 2. And here is another example of a operation on a sequence which is called the reverse in which we just want to reverse the order of the elements in the sequence. So, this is one way of defining it there can be several ways in which we can define the same operation. So, reverse of an empty sequence is always the empty sequence reverse of push x comma s. If I consider the sequence obtained by adding an element x into the sequence s what will this be. So, in this case I will concatenate reverse of s and the sequence obtained by pushing x into the empty sequence. So, what this says is to reverse the sequence obtained by pushing x into x I reverse the sequence s and then append the sequence containing only the element x to the end or concatenate the sequence containing only the element x at the end of the sequence. So, you can similarly define many more complicated operations on sequences. The basic idea is this is very similar to numbers where instead of the next operation that we had with numbers we have the push operation here which puts an element into a sequence. The element could be of any value of a given type t depending on what the type of the sequence is, but all such sequences can be generated using the push operation. And just like for numbers now we can prove properties of these operations. For example, what is the length of the sequence obtained by concatenating s 1 and s 2. So, you can show that this is always equal to the add operation applied to the length of s 1 and length of s 2. Then similarly for any sequences the reverse of reverse of s should give you the sequences. So, this is a property that you can prove formally assuming the definition of reverse that you have used. And again to prove it all you have to do is prove that it holds for the empty sequence and assuming that it holds for a sequence s show that it holds for the sequence obtained by pushing an element x into the s. Similarly, here is another property if I concatenate s 1 and s 2 and reverse the result that should be the same as reversing the second sequence and concatenating the reverse of the first sequence to it. So, here the order s 2 and s 1 changes in that. Note that concatenate is not a commutative operation unlike the addition operation. The order matters in this case. So, try proving these exercises and we will look at some more operations on sequences later. Thank you.