Hello World in Elixir

Hello World in Elixir Featured Image

Welcome back to another installment of Hello World in Every Language. Today, we’re going to take a stab at Hello World in Elixir, a general-purpose programming language released in 2011.

Table of Contents

Elixir Background

According to WikipediaOpens in a new tab., Elixir is yet another general-purpose language with functional tendencies. That said, Elixir is quite a bit different from some of our recent languages. For starters, Elixir runs on the Erlang virtual machine known as BEAM. This means that Elixir actually compiles down to bytecode much like Java.

In terms of features, Elixir has a lot to offer. For instance, Elixir has support for macros which modify the abstract syntax tree. You may recall that Rust has the same feature.

In addition, Elixir offers Python-like doc strings which leverage Markdown. This is a great feature for automatically generating documentation.

Another cool feature of Elixir is exactly what I found interesting about Scala: everything is an expression. In other words, everything evaluates to a value.

Finally, my favorite feature of Elixir has to be pattern matching. In Elixir, we can match patterns using the same operator most languages use for assignment: =. For example:

myVal = 5
5 = myVal  # A valid match
6 = myVal  # (MatchError) no match of right hand side value: 5

In this example, we assign myVal a value of 5. Or in Elixir terminology, we actually bind a value of 5 to myVal through pattern matching. Then, we compare 5 to myVal which is a valid match. With that in mind, it’s clear why we get an error when we try to match 6 to myVal.

Of course, pattern matching gets much more fun than that. We can also use pattern matching to destructure other data types:

{x, y, z} = {:hi, 117, "some string"}
x  # :hi
y  # 117
z  # "some string"

In addition, there are several other ways pattern matching can be used, but we have to get to our implementation of Hello World in Elixir,

Hello World in Elixir

Alright, let’s get right to it:

IO.puts "Hello, World!"

As we can see, Hello World in Elixir is just a single line of code. As usual, let’s dig into it a bit.

Up first, we have a reference to the IO moduleOpens in a new tab.. In Elixir, the IO module is the standard tool for working with standard input and output as well as files and other devices. So, it makes sense that we’d use it here to gain access to standard output.

Up next, we call the puts function of the IO module. Like print in most languages, puts simply writes a value to standard output. In fact, we aren’t limited to standard output. We can redirect the output to other streams such as standard error:

IO.puts :stderr, "Uh Oh!"

At any rate, puts, in our primary example, will simply write “Hello, World!” to the user. To be honest, I’m surprised this is only the second time we’ve seen the puts keyword in this series—the first being Ruby.

How to Run the Solution

As always, if we want to give the code above a try, we can use an online Elixir editorOpens in a new tab.. Copy the code into the editor and hit run.

Alternatively, we can run the solution locally if we download the latest version of ElixirOpens in a new tab.. After that, we’ll want to get a copy of the solutionOpens in a new tab.. Assuming Elixir is in our path, all we have to do is run the following commands from the command line:

elixir hello-world.ex

If successful, the “Hello, World” string should print to the console.

Sample Programs in Every Language

And, that’ll do it! I think that just about covers functional languages for now. Once again, if you want to see a language that I haven’t included, drop it in the comments. Also, if you liked this article, feel free to give it a share.

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