Erlang (programming language)

From Bauman National Library
This page was last modified on 8 June 2016, at 20:57.
Revision as of 20:57, 8 June 2016 by pavel shagaev (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Paradigm multi-paradigm: concurrent, functional
Designed by Joe Armstrong, Robert Virding, and Mike Williams
Developer Ericsson
Stable release 18.3[1] / March 16, 2016; 6 years ago (2016-03-16)
Typing discipline dynamic, strong

Apache License 2.0 (since OTP 18.0)

Erlang Public License 1.1 (earlier releases)
Website [[1]]
Major implementations
F#, Clojure, Rust, Scala, Opa, Reia, Elixir, Dart, Akka

Erlang (Erlang) - a functional programming language that allows you to write programs for different kinds of distributed systems. Developed and maintained by Ericsson. Language includes means of generating the parallel processes and their communication by sending asynchronous messages. The program is translated into byte code executed by the virtual machine, which ensures portability.


In the mid-1980s in the computer labs of Ericsson explored the applicability of the existing at the time of programming languages ​​for software telecommunications systems. Joe Armstrong (Joe Armstrong), Robert Virding (Robert Virding) and Mike Williams (Mike Williams) under the guidance of Bjarne Dekker (Bjarne Däcker), writing a prototype programs in various languages, came to the conclusion that none of these languages ​​do not have a complete set capabilities needed in telecommunications systems. The result is a new programming language - Erlang. The name of the language, probably, was in honor of the Danish mathematician and engineer Agder Erlang, the founder of scientific direction for the study of network traffic in telecommunication systems. According to another version, the name of the language was originally an acronym for «ericsson language».


Language is dynamic. The syntax for inheritance by Prolog - compact, contains a minimum of designs, easy to master, but not primitive. It offers the programmer - modules polymorphic functions, pattern matching, anonymous functions, conditionals, structures, exception handling, optimizing tail recursion. In general, the basic arsenal of modern functional languages. The functional paradigm (no assignment) allows Erlang avoid such traditional imperative languages ​​problems of distributed applications as the need for synchronization, the risk of deadlocks and race. The main thing in Erlang - his model of lightweight processes. To paraphrase Erlang's slogan of the day "Everything is an object" («Everything is an object"), you can say "Everything is a process" («Everything is a process"). Processes are cheap, the creation process does not take more resources than a function call. The only way to process interaction is asynchronous messaging. The process has a "mailbox" where can selectively read the messages, what helps pattern matching, validation code "box" something like a program on awk, right shovel and processed, the rest remains or ejected. The development programs impose a strong imprint two points ^ language stimulates the creation of a large number of competitive processes processes severely isolated and have no general state The design process is iterative partitioning system hierarchy competitively interacting subsystems as components become quite simple to implement. Power cords "Processes + Posts" no less than "lens + interface + Inheritance", but often leads to a more compact and comprehensible decisions. Eliminating competition is as easy as creating. No need to lock access to the state of the process to synchronize their interaction much easier life. For competitive resource usually just created a process monitor, through which the interaction with the resource. The second point is expressed in the formula «let it crash» («let the process will fall"). Instead of intercepting errors and attempts to continue the work of the program, comprising a risky code is allocated to a separate process of suicide, this process is doing everything possible for the system to kill him in case of an error, and the parent process only receives reports about the death of the hapless offspring and makes conclusions. Given that typically 90% of such situations is nothing more worthwhile than display an error message and try to live with the pain more, it is not possible, the lack of multiple checks in the code is not so unattractive.

The high-level design

Erlang is a declarative programming language that is used to describe most of what is to be computed rather than how. For example, the function definition, which uses pattern matching to select one of the options of calculating or extracting data element of the composite structure, reminiscent of the equation. Pattern matching is common even on the bit line, which simplifies the implementation of communication protocols. Functions are first-class objects in Erlang. The language is also widely used for specific functional programming paradigm inclusion list comprehensions (generators lists).

Parallel Computing

The exchange of messages between processes in Erlang A distinctive feature of the language is the use of lightweight processes in accordance with the actor model. This approach allows to perform simultaneously hundreds of thousands or even millions of these processes, each of which may have a modest memory requirements. Processes are isolated from each other and have a common state, but between them may be reached and receive messages on their status. For the interaction process uses asynchronous messaging. Each process has its own message queue, the processing which uses pattern matching. The process of sending a message does not receive a delivery notification, even if the process ID of the recipient is invalid or the recipient ignores the message. Thus, the responsibility for properly organized communication between processes is on the developer. For example, in the implementation in Erlang network chat program structure can directly reflect both user actions on the exchange of messages by starting new processes. Effective communication is maintained and when the number of processes and memory requirements are minimized due to the fact that lightweight processes to manage the virtual machine, rather than a means underlying operating system.

Distributed Computing

Erlang was designed from the outset for distributed computing and scalability. Distribution of computations embedded in the syntax and semantics of the language, so the construction of the system can be carried out, abstracting from the specific location calculations. In the standard Erlang can establish communication processes TCP / IP protocol, regardless of their underlying supported platforms (operating systems). A working copy of the runtime Erlang (Eng. Erlang runtime system) is called a node (Eng. Node). Programs written in Erlang, are able to operate on multiple nodes. Nodes can be processors, many of the core processor, and even a cluster of machines. The node has a name and "knows" about the existence of other components on a given machine or network. Create processes and interaction of different nodes is no different from the organization of interaction processes within a node. To create a process on another node process enough to know his name and, without good reason, it may not be interested in the physical location of the associated process. Syntax process of sending a message to his host and the remote is the same. With built-in language features distributed computing clustering, load balancing, adding nodes and servers, increasing the reliability of cause only small costs for the additional code. Default units are designed to operate within a separate network segment (DMZ), but, if necessary, communication between nodes can be protected using cryptographic techniques to SSL.

Soft real time

Programme a high-level language Erlang can be used in systems of soft real-time (which is sometimes translated as "a pseudo" or "quasi"). Automatic memory management and garbage collection are in the same process that makes it possible to build systems with millisecond response time (even though the need for garbage collection) without experiencing deterioration of throughput at high load.

Hot swapping of code

For systems that can not be stopped to update the code, Erlang offers hot swapping code (Eng. Hot code upgrade). In this case, the application can run simultaneously old and new versions of the code. In this way the software in Erlang can be upgraded without downtime, and identified errors are corrected.

Data Types

Typing in Erlang is a strong and dynamic. Dynamic typing has been selected for the Erlang language due to the fact that the first developers was more familiar with it. According to Joe Armstrong, static typing would require very large expenditures of labor, in particular, to implement a system of hot code reloading it would be extremely difficult. Such typing at which types of errors are detected only at run time, however, has not prevented to create systems with very high levels of availability. Data in Erlang are immutable: the operation does not overwrite the old values ​​in memory. If necessary, the modules can be implemented in Erlang descriptions and definitions of new types (not affect the compilation of the program) to automatically check with the types utility Dialyzer.

Syntax elements

Comments  %
Case sensitive yes
Assigning a value to a variable =
Defining functions fun(a, b) -> ... end
Call functions f(a,b,...)
Calling a function without parameters f()


Обозначение Designation Example Result example
bnot Bitwise negation bnot(2#1000) -9
band Bitwise AND 2 band 3 2
bor Bitwise OR 1 bor 2 3
bxor Bitwise exclusive OR 5 bxor 3 6
bsr Bitwise right shift 32 bsr 2 8
bsl Bitwise left shift 1 bsl 5 32


Hello, World!

Example for versions erl 5.7.3 The first string is the name of the module, and indicates that it should be in a file prog.erl. The second line exports the 0-ary (no parameters) of main. The third describes the actual function that displays "Hello, World!".

main() -> io:format("Hello, World!~n").


Example for versions erl 5.7.3 It uses recursive definition of factorial. In Erlang has no built-cycles, so the cycle is necessary to simulate the recursive function that starts with high values of N, but makes a recursive call for the N-1 before it displays N !. loop (_) - a branch that sets the behavior of the function when its argument - is not an integer or negative (necessary for the correct definition of the function).


-export([main/0, loop/1]).fact(0) -> 1;
fact(N) -> N * fact(N-1). loop(N) when is_integer(N), N>=0 -> 
    io:format("~B! = ~B~n",[N,fact(N)]);
loop(_) -> ok.main() -> loop(16).

Fibonacci numbers

Example for versions erl 5.7.3 Uses an iterative definition of Fibonacci numbers, expressed in the form of tail recursion.

-module(prog).-export([main/0]).fib(1,_,Res) -> 
    io:format("~B, ",[Res]);
fib(N,Prev,Res) when N > 1 -> 
    io:format("~B, ",[Res]),
    fib(N-1, Res, Res+Prev).main() -> 

Fibonacci numbers

Example for versions erl 5.7.3 Used Binet's formula. Floating point numbers are required to be displayed with at least one decimal place, so the result of the work is as follows:

1.0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 21.0, 34.0, 55.0, 89.0, 144.0, 233.0, 377.0, 610.0, 987.0, ...

-module(prog).-export([main/0]).fib(0) -> ok;
fib(N) -> 
    SQ5 = math:sqrt(5),
    T1 = math:pow(0.5*(1 + SQ5),N),
    T2 = math:pow(0.5*(1 - SQ5),N),
    io:format("~.1f, ", [(T1-T2)/SQ5]).main() ->

Quadratic equation

Example for versions erl 5.7.3

-module(prog).-export([main/0]).solve(A, B, C) -> 
    D = B*B - 4*A*C,
    if (D == 0) -> io:format("x = ~f~n", [-B*0.5/A]);
    true -> 
        if (D > 0) -> 
                SQ = math:sqrt(D),
                io:format("x1 = ~f~nx2 = ~f", [(-B+SQ)/2/A, (-B-SQ)/2/A]);
        true -> SQ = math:sqrt(-D),
                io:format("x1 = (~f,~f)~nx2 = (~f,~f)", [-0.5*B/A, 0.5*SQ/A, -0.5*B/A, -0.5*SQ/A])
.main() -> 
    case io:fread("A = ", "~d") of
    eof -> true;
    {ok, X} ->
        [A] = X,
        if (A == 0) -> io:format("Not a quadratic equation.");
        true -> 
        case io: fread("B = ", "~d") of
            eof -> true;
            {ok, Y} ->
                [B] = Y,
                case io: fread("C = ", "~d") of
                eof -> true;
                {ok, Z} -> 
                     [C] = Z,
                     solve(A, B, C)


  1. Erlang OTP 18.3 has been released