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"

-------------------- PARSE ERROR ------------------------
Something went wrong while parsing an `if` expression in 
repl_value_2's definition.

5|   if velocity > 11.186 then "Godspeed"
6|
   ^
I was expecting:

  - an `else` branch. An `if` must handle both possibilities.
  - an argument, like `name` or `total`
  - the end of that `if`. Maybe you forgot some code? Or maybe the body of
    `repl_value_2` needs to be indented?

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"

Note: As mentioned in chapter 1, elm repl automatically adds a vertical bar (|) 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 to writing code in a file in the next section.

Back to top
Close