2.12

elm repl

elm repl is a tool that lets us experiment with Elm. We will be using this tool extensively in the next chapter to learn the syntax and semantics of the Elm programming language.

REPL stands for Read Eval Print Loop. It basically waits for us to enter some Elm code, evaluates it, prints out the result, and goes back to waiting for more code. This cycle repeats forever until we exit out of it by pressing Ctrl + d.

Note: Another way of exiting an elm repl session is by typing :exit and hitting enter.

Like elm reactor, elm repl allows us to type Elm code and see the result immediately without having to compile it manually. The difference between elm reactor and elm repl is that the former takes in an Elm file, compiles it to JavaScript, and renders it on a browser whereas the latter takes an expression in Elm, evaluates it on the fly and displays the result right in the terminal. elm repl is good for trying out short expressions as we explore the language. In contrast, elm reactor is better for trying out a bigger piece of code that’s cumbersome to type in the repl.

Although elm repl works by compiling Elm code to JavaScript, it doesn’t render the compiled code on a browser. Therefore, it needs an alternative environment to run that code. As explained in the Installation section, Node.js provides that environment.

Running elm repl

Go to the beginning-elm directory in the terminal and run this command: elm repl. You should see something like this:

$ elm repl

---- Elm 0.19.0 ----------------------------------------------------------------
Read <https://elm-lang.org/0.19.0/repl> to learn more: exit, help, imports, etc.
--------------------------------------------------------------------------------
>

We can now type all sorts of Elm expressions in here and find out what they evaluate to. Here are some examples:

> 42 / 7.5
5.6 : Float

> pi
3.141592653589793 : Float

> List.reverse [ "Next", "Stop", "Pottersville" ]
["Pottersville","Stop","Next"] : List String

Note: All examples in this book are thoroughly tested with Elm 0.19. As you work through them, if you notice that the output produced by the repl doesn’t match what’s in the book, try re-starting the repl. Sometimes that fixes the issue. If not, please provide a helpful description of the issue you are experiencing in the comments section.

Back to top
Close