Erlang is a purely functional language that has first class functions and single assignment variables. It is built for concurrency and real time programming and was originally developed for large telephone systems. Despite not having many of the features commonly found in functional and logical languages, currying, higher order functions, lazy evaluation, etc, Erlang makes up for these losses not only with its size and efficiency, but its ease of use which gives programmers used to those language features the feeling they no longer need them.
Contents |
The Basics
Every Erlang source file consists of a module declaration, an declaration of functions to export and the functions themselves. Example:
-module(helloworld).
-export([hello_world/0]).
hello_world() ->
io:format("Hello World~n", []).
The module name must be the name of the source file minus ".erl". Thus, the source file containing the above code would be named "helloworld.erl". Any function found in the list sent to export is able to be called from outside the modules, all others are private to that module. The forward slash zero declares this function takes no arguments. Next, you see the definition of the hello_world function.
Erlang provides a REPL (read, eval, print, loop) that allows you to run Erlang code, compile source files and run functions from those newly compiled Erlang source files which can be started after you have installed Erlang with the command erl. When showing use of the REPL instead of a source file the code will look like:
> c(helloworld). > helloworld:hello_world().
Distinguishable by the '>' before the commands. If you can't tell, the above commands in the REPL compile our previous hello world program, notice the compile function, c, takes the module name no the source file name, and then runs the exported function hello_world.
Before continuing there are some basic syntax rules that needs to be explained. This gives many new Erlang programmers trouble if it is not explained before they dive into writing code. There are 4 ways to end a "line" in Erlang, a ",", a "." a ";" and nothing. A comma corresponds to "and", a semicolon to "or", a period to "end" and nothing is used when that line is followed by an "end" Here are a few examples:
world(hello) ->
io:format("hello world", []);
world(goodbye) ->
io:format("goodbye world", []).
This example shows a use of the semicolon as an "or". We have not discussed atoms yet, but rest assured that calling helloworld:world(hello) and helloworld:world(goodbye) will call the proper functions. The reason is that the function call tries to match the first declaration of world and because of the semicolon at then end of that function it knows it can also match the next declaration of world Thus, its saying, this function OR this function.
hello_goodbye() ->
io:format("hello world", []),
io:format("goodbye world", []).
In hello_goodbye you see the use of the comma saying, hello_goodbye is this first line AND the second line. Of course we could add a comma and another line after the second, and so on.
hello_goodbye(Atom) ->
case Atom of
hello ->
io:format("hello world", []);
goodbye ->
io:format("goodbye world", [])
end.
I don't want to completely explain this function until later. I just hope you can tell how lines are ended by looking at these above examples. This function just matches a function argument with hello OR (;) goodbye. Since the line that prints "goodbye world" is followed by "end" it has nothing after it. This is because we are not continuing with this 'case' clause (we are not matching Atom against end) so it is not "OR end" nor is it "AND end". It is just end.
Hopefully this makes sense. This needs to be understood early so that the rest of the articles examples will make sense with out having to explain these details while trying to explain another part of the language. And trust me, this is better than just ending every expression with a semi colon or relying on indentation.


