Hello Erlang
Table of Contents

I've been searching the web for good blogs, tutorials, and books on Erlang, and while I've found several, none of them cover the kinds of programs I'm interested in. To put that another way, I can't find a lot of fun Erlang programs to explore even though I know it's possible to write them.

The purpose of this blog is to change that. I'm going to explore Erlang with game programming. But before we get there, we need to cover some basics first. Today, we will cover the following skills:

  1. How to start the Erlang Shell
  2. How to output "Hello Erlang"
  3. How to shut Erlang down
  4. How to use Erlang as an awesome calculator
  5. How to find Erlang's advanced math library
  6. How to use some other feature's of Erlang's Shell

This web-book is all about immediate gratification, and the best way I know to explore Erlang's shell in a way that is immediately useful is to use the shell as a calculator. So let's begin.

First, go to your shell or command prompt. If you use Windows, you may know this as the dos prompt. Next, let's fire up Erlang's interpreter. The interpreter is a program that reads the code you write and makes it work. It's a bit like your web browser which reads the html code I'm writing right now and turns it into readible text on your computer screen.

To turn on Erlang's shell, I type: erl(what I type is in red) at my command prompt (which looks like a dollar sign on my computer) like so:

$ erl

If erlang is properly installed on your computer, then something like this should appear on your monitor as Erlang starts up and begins to run...

Eshell V5.7.2  (abort with ^G)
1>

Ok. So, now we have opened up the shell and started Erlang. Great! Go grab a cookie because that's progress.

Following tradition, let's run the "Hello World" program in Erlang. Go back to the shell and type the following:

Eshell V5.7.2  (abort with ^G)
1> io:format("Hello World\n").
Hello World
ok
2>

If it worked, it means you are in business. If not, then double check what you wrote to make sure you entered exactly what I did. If something went wrong, I'm betting you forgot to add the period on the end. You MUST add the period!

Now, let's see how to exit the Shell. On line 2, type the following:

Eshell V5.7.2  (abort with ^G)
1> io:format("Hello World\n").
Hello World
ok
2> q().
ok
3> clay@Hal $

You should have just exited Erlang. So now you know how to start Erlang, perform basic output, and exit Erlang. The next step is learning how to use Erlang as a calculator. Fire up Erlang's shell and follow along:

Eshell V5.7.2  (abort with ^G)
1> 10 + 10. 20 2>

Let's look at what just happened. Erlang's interpreter gave us a line named >1 to enter a command on. I typed 10 + 10. and Erlang responded with the answer 20 and then gave us a new prompt named 2 for us to enter a new command on. Those line numbers are handy because it makes it easy for us to discuss specific commands. For example, we added 10 + 10. on line 1.

Important Safety Note: Erlang likes English punctuation and gets snooty if you omit it. You must end equations with a period; otherwise, Erlang will just keep printing 1> over and over and over until you do.

Let's keep experimenting with math:

Eshell V5.7.2  (abort with ^G)
1> 10 + 10.
20
2> 987654321 * 123456789.
121932631112635269
3> 37 / 5.
7.4
4> 37 div 5.
7
5> 37 rem 5.
2
6> (10 * (25 + 100)).
1250

Notice that div and rem perform integer arithmetic (math on numbers without decimal points). You probably remember doing integer arithmetic in grade school. Believe it or not, integer arithmetic is actually pretty useful for some things. For right now though, simply experiment with the Erlang shell. This shouldn't take long, but by the time you're done, you will have a very powerful calculator that can handle math problems far larger than any pocket calculator you'll ever find. Here are the math operators we've seen so far:

All pretty simple, but also pretty basic. What about sin and cosine and taking one number to the power of another? Well, Erlang makes all that stuff simple too. But to use those features, we have to import Erlang's math module. We'll talk about modules in greater detail later. For now, just relax and focus on the fact that Erlang modules are easy! We just prepend the math function we want with the name of the math library, which happens to be--helpfully enough--math. Follow the patterns below to see how it works (I'll pick back up at line 7):

7> math:pi().
3.141592653589793
8> math:sqrt(100).
10.0
9> math:pow(10,2).
100.0

Get the picture? To call a specific function within the math module, we just say math:some_function(). (Don't forget that period at the end of the function call). Of course, we first have to know what functions the math module contains. Fortunately, Erlang makes it easy to find out that information. The Erlang shell comes with a bunch of built-in commands. One of those built-in commands is the m(). command that I use all the time. Now we'll look up the math module using the m(). command on line 10. Go ahead and type m(math).

10> m(math).

Hopefully, Erlang just printed off a ton of math functions contained inside the math module.

When I was first starting out, my first question was to ask what was with all the /0 /1 and /2 nonsense following each function. As it turns out, that information is wonderfully useful. This is called ARITY and it's the way Erlang tracks how many arguments should be passed to a particular function. I've seen quite a few explanations of Arity now, and my personal feeling is that it's one of those concepts that is simple to show, but difficult to explain. So, rather than try to explain Arity--and confusing you--I'm just going to show you how Arity works and everything will look painfully obvious.

Let's examine three functions in greater detail. According to m(math)., we have something called a pi/0 function. That means we have a function that spits out the value of pi and takes zero arguments. Likewise, there's a sqrt/1 function that only takes one argument and pow/2 which requires two arguments. If that doesn't mean much, it's ok. Just keep following along and everything will click.

Let's look at those function calls at lines 7-9 again:

7> math:pi().
3.141592653589793
8> math:sqrt(100).
10.0
9> math:pow(10,2).
100.0

Notice how line 7 calling math:pi() doesn't take any arguments. That's what we mean we say it has an arity of zero. Line 8 calls the square root function with one argument; math:sqrt(100) because it has an arity of one. Finally, Line 9 calls the power function with two arguments math:pow(10,2) because it has an arity of two. Are you starting to see what we mean by Arity?

You now know the three important things about calling functions:

  1. You must get their name right
  2. You must name their module correctly
  3. You must include the proper number of arguments inside their parentheses

Finally, to quit Erlang's shell, simply type q(). to get out. We'll issue the command on line 11 like so:

11> q().
ok
12> clay@Hal $

If you've made it this far, then bravo! That's a ton to learn in one day. We'll look more at finding, exploring, and then writing modules in Erlang later on. We'll also explore more about how Erlang The Language works. But for now, we've covered quite enough to have discovered a wonderfully useful calculator. In the next post, we'll expand on the calculator by learning more Erlang. For now though, just experiment with what we've covered.

Next Stop: Playing with Variables