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 a substring is part of the seq_to_sum parameter. Its default value is the global constant ST_ALLNUM.

    If substring_escape is set to the default value of ST_ALL_NUM, any substring will be considered an atom, but if the parameter is set to ST_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.e
sequence seq1, seq2
seq1 = {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) then
printf(1,"they are equal")
else
printf(1, "they are not equal")
end if

Explanation

  • Line 1: We import the stats.e file in our code, because sum() is part of the library provided by this file.

  • Line 3: We declare the variables, seq1 and seq2.

  • Lines 5 and 6: We assign values to seq1 and seq2.

  • Lines 8–10: We print the sums of seq1 and seq2.

  • 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 if statement block and check the equality of the sums of seq1 and seq2.

Free Resources