What is a sequence in Euphoria programming language?
Introduction
In Euphoria there are two main data types:
- Atom
- Sequence
Atoms are single characters while a sequence can be an atom, a set of atoms, and can even contain another sequence. The contents of a sequence are enclosed in a pair of curly brackets {}
Example of a sequence
-- declare the sequence
sequence seq1
seq1 = { 2,3,4,5}
The contents of a sequence can be accessed using their index. The index of a sequence, unlike arrays in other languages, begins with 1 for the first element.
We can select a single element from the sequence above using its index:
printf(1,"%d", seq1[3])
-- declare the sequencesequence seq1seq1 = { 2,3,4,5}printf(1,"%d", seq1[3])
The value returned is the third element in the sequence seq1 defined above. The "%d" will cause the element to be formatted as an integer. Meanwhile, a "%f" causes it to be formatted as the float.
A special type of Sequence
A character string is a special type of sequence because it is implemented by enclosing characters inside double quotation marks or by using a raw string notation method. The raw string notation method involves having the set of characters inside a single-quote, double-quotes or triple-quotes. Let us see some examples below.
-- declarationsequence word1,word2, word3--using double quotesword1 = "Hello"--single quotesword2 = 'World'-- enclosed inside triple double quotesword3 = """It a bright day"""
Note that the words after the double dash (
--) are comments.
Sequence nesting in Euphoria
Just as arrays in other languages can contain another array inside them, sequences in Euphoria can contain other sequences. They can contain both the usual sequence enclosed in curly braces and the special sequence inside double quotation marks. The elements of these nested sequences can be accessed using their index numbers. Let us see some examples.
-- declarationsequence nesting-- assigning valuesnesting = { "Hello", 2, {60,80}}-- display any elementprintf(1, nesting[1][2])puts(1, "\n")printf(1,"%d", nesting[3][2])
In the example above, a nested sequence nesting had other sequences "Hello" and {60,80} inside it. Using the printf() function, the elements of the other sequences it contained are targeted and displayed on the screen.
Operations on a Euphoria sequence
There a lot of operations that can be performed on a sequence. Such operations include:
Unary operation
This involves using a single operator to perform an action on all elements in a sequence. For example:
sequence junejune = -{40, 50, 60}for x = 1 to length(june) doprintf(1, "value of june[%d] = %d\n", {x, june[x]})end for
In the code above, all elements of the sequence are negated simply by placing the minus sign in front of the sequence. Using the for loop, all values of the sequence are then printed as output.
Arithmetic operations
Basic arithmetic operations like addition, subtraction, multiplication, and division can be performed on a sequence. This type of operation is not unary because it requires two sequences of equal length for it to be performed. Let us perform an addition operation on two sequences.
-- declare some sequencessequence nine, ten, numnine = {10, 20, 30}ten = {50, 60, 70}num = nine + tenputs(1, "Value of num = {")for x = 1 to length(num) doprintf(1, "%d,", num[x])end forputs(1, "}\n")
In the example above, we use the loop to print the value of the addition on the screen, using the curly braces.
Code
-- declare some sequencesequence first, second, addSeqfirst = {10, 20, 30}second = {50, 60, 70}addSeq = first + secondputs(1, "Value of addSeq is ")print(1,addSeq)
Explanation
Three variables addSeq , first and second of type sequence were defined in the above code. Due to the addable nature of two sequences, variable first and second are added and the output saved in addSeq which is printed to screen. addSeq will have length same as that of variables first or second just as shown in the output of the code.
Remember that for this addition to work the adding sequences have to be of the same length.