Erlang

Erlang is a "Concurrency Oriented Programming" language.

Learning the Language

Recommended resources for learning Erlang:

As backup to the above, there are other online documents, even a book on Erlang, but you shouldn't need these for this course.

Using the Language

Erlang can be installed easily on Windows machines, downloading the latest version from here. For installing on Linux, Unix or Mac, the situation is less clear. I did manage to install it on Linux, but it was far from simple. An executable version of Erlang is available on the department's Linux cluster, following instructions below.

Key Idea: the "Path" environment variable

Whether you use a Unix/Linux version or a Windows version, the default behavior (which we don't like) is that Erlang programs would be located in the same directory as the Erlang "program files" (binary executables, etc) -- rather than allowing you to put your own Erlang programs in your own directory. Here is how to get around this default behavior.

Windows: suppose you have installed Erlang, and all the program files have been put into C:\Program Files\erl5.5.5\ -- the "erl" compiler will be in the bin subdirectory of this. Here is how to set up your Windows account so that it will automatically find Erlang program files, even within your own directory:

Once this is done, you can start a Command Prompt, and position the current directory to wherever you'd like to have your Erlang program files. Once there, you execute the erl command and for example "c(test)." should attempt to compile the file test.erl in the current directory (assuming you have made a test.erl file). I created and editted erl programs using Notepad, but other editors would be OK also.

Department Linux Cluster: I'm supposing that you are using the default shell for CS department accounts, tcsh or csh or something like that (the alternative would be bash). What you need to do is change the PATH environment variable. Check to see if you have a file in your home directory called ".cshrc". You should have this file, which has commands and parameters for each time you log in. Add this line to the file:

That line appends /space/herman/public/bin to your PATH variable (you may need to log out and log back in for this change to take effect). Check your results with the command

-- you should find the /space/herman/public/bin listed in the reponse output. Then try the command

and you should see a prompt from Erlang. You're all set.

Example Erlang Programs

I'll be adding example Erlang programs here; you can also look at

BIFs (Built-In Functions) and Applications

The Erlang Manual has a standard library index where you can browse built-in functions.

The io module

One built-in you'll probably want to use is the io module. Example:

The io module also has something similar to the printf formatting string with substitutions (also recently added to Java).

The gs module (for widgets)

This is optional and not well documented. The users guide is currently not much more than an index, but with enough patience and experimenting, you probably can get windows, buttons, colors, and so forth to work.

Tools

There are lots of tools for developing Erlang programs. You can get some idea of the possibilities (though it's confusing to use this without reading documentation), by trying toolbar :

Try the above after starting Erlang with the erl command, and a window should appear with lots of trace/debugging features.

Debugger Module

The debugger module might be helpful to find problems in your programs.

The Parser Module

The parsetools module makes it easy to parse with a BNF grammar.

Web Tools

The webtool module is for building a simple web server. (In the other direction, recall that the small examples list contains an example to fetch a page from a webserver.)

Other Examples

I haven't looked through the code available in user-contributed examples; some of them could be useful for advanced applications.

The Reading Assignment

You can find some examples in the erlang reading assignment page.

Simple Concurrency

Here's a simple example, adapted from the online course, showing how processes can send messages in a call/response pattern, something like a subroutine call:

Notes:

  1. Ending a process with the atom true is expected, it seems.

  2. Starting a process has three parameters:
    •      spawn ( <name of module> ,  <name of exported function> , <parameter> )
           
      Notice that if the exported function has no parameter, use [] for that.
  3. It's a good idea to put output statements when learning.

Exercises Generalize the above.

Debugging Concurrency

Here is an example that really is not concurrent, but uses Erlang process sending and receiving messages to compute factorial. Let us suppose that the file concurrentFac.erl contains the following code:

Now imagine that we're at the keyboard, and enter the Erlang shell (say, by invoking the erl command). We enter the following:

The first line, c(concurrentFac, debug_info), compiles the program with debugging information added, which enables using the debugger module. Line 2 starts the debugger, which opens a window (to be demonstrated in class). Line 3 then starts concurrentFac:fac(5), under control of the debugger. Then we can step through execution to see what is happening. The Erlang user manual has a chapter about the debugger.

Palindrome Example

This erlang palindrome example was presented in class.

erlang (last edited 2007-12-01 19:42:24 by Ted Herman)