What is the compare() method in Euphoria?

Overview

The compare() method compares two items and returns values indicating less than, equal to, or greater than as a result of the comparison.

Syntax

compare(value_to_compare, reference_value)

Parameter

  • value_to_compare: This is a value of an object data type that we want to compare to another value.
  • reference_value: This is the value of an object data type that serves as a reference point for the comparison.

Return value

This method returns an atom, which can be -1, 1 or 0.

  • 0 : If compared objects are identical.
  • -1 :If value_to_compare is less than reference_value.
  • 1: If value_to_compare is greater than reference_value.

Example

print(1,compare("group","group"))
puts(1,'\n')
print(1,compare("GrouP","group"))
puts(1,'\n')
print(1,compare("groups","group"))
puts(1,'\n')
print(1,compare("group","grouping"))

Explanation

The puts() method in the code snippet is used to print a new line so that each code will execute on a line new. For each of the comparisons, the first parameter of the compare function is the value that will be compared with the second value which serves as the reference value.

  • Line 2: The output of the comparison here will be 0 because the compared values are identical.
  • Line 6: Two words that are the same in spelling but different in terms of the different letter case when compare still returns as -1, which indicates that the compared value is less than the reference value. The same is the case of code in line 12.
  • Line 10: We compare two values on this line where the compared value is greater than the referenced value, leaving the function to return 1.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources