Hello World in Elm

Hello World in Elm Featured Image

Hello and welcome back to another installment of the Hello World in Every Language series. At this point, we’ve covered over twenty languages, and I have no plan to stop. Today, we’re looking to implement Hello World in Elm, a fun web development language that appeared in 2012.

Table of Contents

Elm Background

According to WikipediaOpens in a new tab., Elm is a functional programming language used for building web-based graphical user interfaces.

In terms of features, Elm offers immutability, static typing, and a module system.

Static Typing

At this point in the series, we’ve seen a lot of languages which support static typing. Because of that, I’m slowly starting to draw a distinction between statically and dynamically typed languages.

Originally, my understanding of static typing came from languages like Java and C where we have to explicitly state the types of our variables:

// Java Explicit Typing Example
int x = 5;
String helloWorld = "Hello, World!";

Meanwhile, my understanding of dynamically typed languages came from Python where variables don’t need type annotations:

# Python Implicit Typing Example
x = 5
hello_world = "Hello, World!"

Because of this rather naive distinction, I was under the impression that Elm couldn’t actually be statically typed because the language supports type annotations. Instead, I thought that maybe Elm was more like Hack, a gradually typed language.

Of course, the true distinction between static and dynamic typing is at what point the type checking occurs: before or at runtime. In other words, languages don’t need explicit typing to be statically typed. Likewise, languages don’t need implicit typing to be dynamically typed.

In the case of Elm, type inference is used to determine value types. However, type annotations can be used to improve program readability. And, the compiler will even verify the annotations are correctOpens in a new tab.. However, the annotations, in this case, have nothing to do with gradual typing.

Immutability

While debating type systems is great, Elm’s most interesting feature to me is immutability. Immutability—a byproduct of value semanticsOpens in a new tab.—describes a value’s inability to be changed. In other words, immutable values are values that cannot be changed after creation.

To me, the concept of immutability is interesting because code like the following will fail in Elm:

a = 10
a = a - 5

Because a is immutable, the code above will actually cause a recursion error. You can read more about that in Elm’s documentation. At any rate, let’s get to the solution!

Hello World in Elm

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.

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 ElmOpens in a new tab.. 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 GitHubOpens in a new tab.. 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 locationOpens in a new tab. in a browser and open the HelloWorld file. That’s it!

Alternatively, use an online Elm compilerOpens in a new tab. for testing code snippets. Give it a go!

Sample Programs in Every Language

As always, thanks for sticking around. So far, I’m finding Elm to be a lot of fun to learn, and that’s likely because I’ve been playing with a lot of functional programming languages for the first time.

At any rate, if you liked this article, give it a share. Oh, and if you want to recommend a language for a future article, use the comments below or fork the GitHub repo. Thanks, again!

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts