Elm – Operators

elm operators

In this guide, we will discuss Operators in ELM. An operator defines some function that will be performed on the data. The values on which the operators work are called operands. Consider the following expression

7 + 5 = 12

Here, the values 7, 5, and 12 are operands, while + and = are operators.

The major operators in Elm can be classified as −

  • Arithmetic
  • Relational
  • Logical

Arithmetic Operators

Assume the values in variables a and b are 7 and 2 respectively.

Show Examples

Sr. No.OperatorDescriptionExample
1+(Addition)returns the sum of the operandsa+b is 9
2-(Subtraction)returns the difference of the valuesa-b is 5
3* (Multiplication)returns the product of the valuesa*b is 14
4/ (Float Division)performs division operation and returns a float quotienta / b is 3.5
5//(Integer Division)performs division operation and returns a integer quotienta // b is 3
6% (Modulus)performs division operation and returns the remaindera % b is 1

Relational Operators

Relational Operators test or define the kind of relationship between two entities. These operators are used to compare two or more values. Relational operators return a Boolean value, i.e. true or false.

Assume the value of a is 10 and b is 20.

Show Examples

Sr. No.OperatorDescriptionExample
1>Greater than(a > b) is False
2<Lesser than(a < b) is True
3>=Greater than or equal to(a >= b) is False
4<=Lesser than or equal to(a <= b) is True
5==Equality(a == b) is false
6!=Not equal(a != b) is True

Comparable Types

Comparison operators like >= or < work with comparable types. These are defined as numbers, characters, strings, and lists, tuples. The comparable types on both sides of the operator must be the same.

Sr. No.Comparable TypeExample
1number7>2 gives True
2character‘a’ ==’b’ gives False
3string“hello” ==”hello” gives True
4tuple(1,”One”)==(1,”One”) gives True
5list[1,2]==[1,2] gives True

Open the elm REPL and try the examples shown below −

C:\Users\admin>elm repl
---- elm-repl 0.18.0 -----------------------------------------------------------
:help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl>
--------------------------------------------------------------------------------
> 7>2
True : Bool
> 7.0>2
True : Bool
> 7.0<2.0
False : Bool
> 'a' > 'b'
False : Bool
> 'a' < 'b'
True : Bool
> "a" < "b"
True : Bool
> (1,2) > (2,3)
False : Bool
> ['1','3'] < ['2','1']
True : Bool
>

Logical Operators

Logical Operators are used to combine two or more conditions. Logical operators too return a Boolean value.

Sr. No.OperatorDescriptionExample
1&&The operator returns true only if all the expressions specified return true(10>5) && (20>5) returns True
2||The operator returns true if at least one of the expressions specified return true(10 < 5) || (20 >5) returns True
3notThe operator returns the inverse of the expression’s result. For E.g.: !(>5) returns false.not (10 < 5) returns True
4xorThe operator returns true only if exactly one input returns true. The operator returns false if both the expressions return true.xor (10 > 5 ) (20 > 5) returns false

Next Topic : Click Here

This Post Has 4 Comments

  1. passive income

    It is my belief that mesothelioma is definitely the most fatal cancer. It’s got unusual characteristics. The more I actually look at it a lot more I am convinced it does not act like a real solid tissue cancer. If mesothelioma is a rogue virus-like infection, hence there is the chance for developing a vaccine and also offering vaccination to asbestos subjected people who are at high risk connected with developing foreseeable future asbestos connected malignancies. Thanks for discussing your ideas about this important health issue.

Leave a Reply