Hello World in Elm

Published on 07 May 2019 (Updated: 07 May 2019)

Hello World in Elm

In this article, we’ll implement Hello World in Elm.

How to Implement the Solution

As usual, let’s dive right into the implementation of Hello World in Elm:

module HelloWorld exposing (..)
import Html exposing (text)
main =
  text "Hello, World!"

And, that’s it! We can write Hello World in Elm in just a few lines of code, but what’s really going on in this code?

Up first, we have the module declaration line. In other words, we’ve defined a module with the name HelloWorld. If anyone wanted to use this module, everything would be completely exposed.

In the third line, we import the Html module, so we can access the text functionality. As we can probably imagine, the text function just displays text to the user.

Finally, as with many functional languages, we have the main function. To no surprise, the main function is a special function which provides the starting point for the program. In the case of Elm, the main function must retain an element to draw into the page. In our case, we’re returning a HTML element from the text function.1

How to Run the Solution

Okay, so we have a solution, but how do we run it? Well, Elm is a bit different than our typical languages because it’s for web use only. As a result, we’ll want to download the necessary utilities first.

Now would be a good time to install Elm. With that installed, get the Html package from the command line:

elm-package install elm-lang/html

After that, grab a copy of the elm solution from GitHub. Now, in the same folder as the new file, run the following from the command line:

elm reactor

This will basically launch a local server for testing. Now, go to the local server location in a browser and open the HelloWorld file. That’s it!

Alternatively, use an online Elm compiler for testing code snippets. Give it a go!1


References

  1. J. Grifski, “Hello World in Elm,” The Renegade Coder, 22-Apr-2018. [Online]. Available: https://therenegadecoder.com/code/hello-world-in-elm/. [Accessed: 06-May-2019].  2