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
According to Wikipedia, 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.
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.
// 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 correct. However, the annotations, in this case, have nothing to do with gradual typing.
While debating type systems is great, Elm’s most interesting feature to me is immutability. Immutability—a byproduct of value semantics—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 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:
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!
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!
Kicking off a new series of reverse engineering content inspired by VirtualFlatCAD. Today, we're trying to roll our own uppercase function.
When it comes to capitalizing strings in Python, you have a few options. Use the tools Python provides or roll your own.