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 Wikipedia, 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 module. 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 editor. Copy the code into the editor and hit run.
Alternatively, we can run the solution locally if we download the latest version of Elixir. After that, we’ll want to get a copy of the solution. 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.
Recent Posts
While creating some of the other early articles in this series, I had a realization: something even more fundamental than loops and if statements is the condition. As a result, I figured we could...
Today, we're expanding our concept map with the concept of loops in Python! Unless you're a complete beginner, you probably know a thing or two about loops, but maybe I can teach you something new.