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.

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 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 whereas 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-repl 0.18.0 -----------------------------------------------------------
 :help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl>
--------------------------------------------------------------------------------
>

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

All examples in the book are thoroughly tested with Elm 0.18. 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

New chapters are coming soon!

Sign up for the Elm Programming newsletter to get notified!

* indicates required
Close