What is the sum method in Euphoria?
Overview
The sum() method returns the sum of all the atom values in the sequence supplied to it. It will accept atom values and sum them all up, returning an atom value that is the total of all the atom values.
Syntax
sum(seq_to_sum, substring_escape)
Parameters
-
seq_to_sum: This is a sequence variable containing atom values that will be summed up. -
substring_escape: This is an optional datatype. This parameter is used to indicate what happens when asubstringis part of theseq_to_sumparameter. Its default value is the global constantST_ALLNUM.If
substring_escapeis set to the default value ofST_ALL_NUM, any substring will be considered an atom, but if the parameter is set toST_IGNSTR—another global constant—the substring will be ignored.
Return value
This function returns the sum of all atom values in the sequence.
Example
include std/stats.esequence seq1, seq2seq1 = {1,2,3,4,5,6,7,8,9}seq2 = {10,11,12}print(1, sum(seq1))puts(1,"\n")print(1, sum(seq2))puts(1,"\n")if sum(seq1) = sum(seq2) thenprintf(1,"they are equal")elseprintf(1, "they are not equal")end if
Explanation
-
Line 1: We import the
stats.efile in our code, becausesum()is part of the library provided by this file. -
Line 3: We declare the variables,
seq1andseq2. -
Lines 5 and 6: We assign values to
seq1andseq2. -
Lines 8–10: We print the sums of
seq1andseq2. -
Lines 9–11: We use
puts()to print new lines so that our output could all print on new lines. -
Lines 14–20: We start and end an
ifstatement block and check the equality of the sums ofseq1andseq2.