3.9

If Expression

Let’s say we are embarking on an exciting journey to find out whether Enceladus, one of Saturn’s moons, can sustain life. First, we have to escape the gravitational pull from mighty Earth. If our spaceship can travel with a velocity greater than 11.186 km/s, we can then be free to begin our interplanetary journey. Let’s express this in Elm.

> velocity = 11.2
11.2

> if velocity > 11.186 then "Godspeed" else "Come back"
"Godspeed"

The most common way to express a conditional logic in Elm is through an if expression. It requires three parts:

  • A condition
  • An expression (branch) to evaluate if the condition is true
  • An expression (branch) to evaluate if the condition is false

Unlike some other languages, in Elm we must provide the else branch. Otherwise Elm will throw an error.

> if velocity > 11.186 then "Godspeed"

------------------- SYNTAX PROBLEM -----------------------
I need whitespace, but got stuck on what looks like a new
declaration. You are either missing some stuff in the
declaration above or just need to add some spaces here:

4| someFunction x = x + 1
   ^
I am looking for one of the following things:

    whitespace

The error message above isn’t that helpful. Sometimes Elm can’t explain things in a way we humans can understand. But it’s getting better with each release. So you might see a different (perhaps a better) message with the next major release of Elm.

Like all other expressions, an if expression also returns a value. Let’s capture the return value of the above expression in a constant.

> whatToDo = if velocity > 11.186 then "Godspeed" else "Come back"
"Godspeed"

> whatToDo
"Godspeed"

Some people don’t have the desire to venture out into the unknown corners of Cosmos. But they also don’t like living on Earth. One option for them is to live on a space station that revolves around Earth in perpetuity with horizontal speed of 7.67 km/s. Let’s add this option to our code.

> velocity = 11
11

> speed = 7.67
7.67

> if velocity > 11.186 then "Godspeed" else if speed == 7.67 then "Stay in orbit" else "Come back"
"Stay in orbit"

We can nest as many else if branches as we want. It’s hard to understand what a code does when it’s crammed in one line like that. Elm repl allows us to split code into multiple lines by adding \ to the end of each line and indenting the next line. Let’s try that.

> if velocity > 11.186 then \
|   "Godspeed" \
| else if speed == 7.67 then \
|   "Stay in orbit" \
| else \
|   "Come back"
"Stay in orbit"

As mentioned in chapter 1, elm-repl automatically adds |s in-front of each line starting from the second line. You shouldn’t type them. Also, don’t forget to indent "Godspeed", "Stay in orbit", and "Come back" with at least one space. Elm treats lack of indentation as syntax errors in some cases. We will cover those cases in detail in the Indentation section. Finally, we terminated the lines with \ to make the code more readable, but you don’t have to do that when entering Elm code in a file, which will be covered in the next section.

Even with this ability to type multi-line code, it still is cumbersome to enter longer code in repl. We’ll switch over to writing code in a file soon.

Back to top

New chapters are coming soon!

Sign up for the Elm Programming newsletter to get notified!

* indicates required
Close