Booleans

There are two Boolean values in Elm: True and False. They are both of type Bool.

> True
True : Bool

Put not in front of a Bool to get the opposite value.

> not True
False : Bool

These are useful in conditional expressions. You'll see what I mean in a second.

Comparison operators

The comparison operators work on numbers to produce Booleans. Besides the familiar < less than and > greater than, Elm has operators for <= less than or equal to and >= greater than or equal to, like most programming languages.

> 1 < 2
True : Bool
> 3 > 4
False : Bool
> 5 <= 6
True : Bool
> 6 >= 7
False : Bool
> 7 >= 7
True : Bool

Equality operators

The == equality operator can compare any two values of the same type to produce a Bool.

> 2 + 2 == 4
True : Bool
> 7 == 8
False : Bool

When I said any two values, I meant it. As long as both values have the same type, you're good to go.

> "f" ++ "oo" == "foo"
True : Bool
> "foo" == "oof"
False : Bool

The opposite of == is the /= inequality operator.

> 2 + 2 /= 4
False : Bool
> 7 /= 8
True : Bool
> "f" + "oo" /= "foo"
False : Bool
> "foo" /= "oof"
True : Bool

Conditional expressions

You can use a Bool in conditional expressions using the keywords if and then.

> if 2 + 2 == 4 then "Yep!" else "Nope!"
"Yep!" : String
> if 2 + 2 == 5 then "Something's wrong..." else "It's all good!"
"It's all good!" : String

Unlike in some programming languages, you always need a else part with an if, or you'll get a syntax error.

Logical operators

The logical operators && and and || or let you combine Bools in more complex conditions. If you're already familiar with Boolean logic, I recommend you move on to the next section. If not, let's look at some examples, starting with &&! (Note the parentheses are just to make things easier to read.)

> (2 + 2 == 4) && ("f" ++ "oo" == "foo")
True : Bool

Since 2 + 2 == 4 is True and "f" ++ "oo" == "foo" is also True, the entire expression in turn is True. When we make even one expression False, though, things change.

> (2 + 2 == 4) && ("f" ++ "oo" == "oof")
False : Bool
> (2 + 2 == 5) && ("f" ++ "oo" == "foo")
False : Bool
> (2 + 2 == 5) && ("f" ++ "oo" == "oof")
False : Bool

The || operator is a little different. When the computer sees it, it asks, "Is either expression true?" When both expressions are True, you get the same result as with &&.

> (2 + 2 == 4) || ("f" ++ "oo" == "foo")
True : Bool

But you need to make both False to get a False result.

> (2 + 2 == 4) || ("f" ++ "oo" == "oof")
True : Bool
> (2 + 2 == 5) || ("f" ++ "oo" == "foo")
True : Bool
> (2 + 2 == 5) || ("f" ++ "oo" == "oof")
False : Bool

results matching ""

    No results matching ""