Erlang (programming language)

Erlang is a programming language that was developed at Ericsson by Joe Armstrong, and others. It is named after the Danish mathematician Agner Krarup Erlang, but can also stand for Ericsson language.

  • 2.1 Calculation of the Faculty
  • 2.2 Quicksort
  • 2.3 A small distributed application that runs on two processes Erlang
  • 2.4 Mnesia

Properties

Speaking of Erlang, we mean not only the usually relatively compact language, but also the runtime system and the extensive library. Together, the system Erlang / OTP is called, which OTP is an abbreviation for The Open Telecom Platform. It is a middleware for building distributed, highly available systems. These are, for example, the distributed database Mnesia and CouchDB.

Erlang was originally created for the programming of applications in telecommunications, particularly for exchanges of telephone networks ( switch).

The elements on the special requirements went directly into the design of the programming language and runtime system a:

  • Parallelism
  • High availability
  • Fault tolerance
  • Replacement of modules at run time, etc.

Erlang satisfies the paradigms of functional, concurrent and distributed programming. Joe Armstrong, the spiritual father of Erlang, they referred prefer as Concurrency Oriented Programming Language ( COPL ), which is to be translated roughly as a concurrent oriented programming language and suggests that processes are the most important objects in Erlang. He finds less important, even if it is quite useful for concurrent programming because of the avoidance of side effects, the functional core.

Erlang is one of the few functional programming languages ​​, which are used in the industry. In particular, telephone and network equipment put an Erlang due to its scalability and parallelism.

Processes are implemented very resource-efficient in Erlang. The value written in Erlang Yaws web server with its high scalability, is a good example.

The syntax of Erlang is reminiscent of the Prolog, which is not quite surprising because the system is initially present as written in Prolog interpreter.

Based on the binary format used by the general Erlang binary BERT and a subsequent anabolic Remote Procedure Call protocol was developed.

Included Software

With the Erlang distribution many useful additional tools are included to facilitate the programming of applications clearly:

The standard library also has powerful socket functions for TCP and UDP, and many other small tool functions for lists, binary data, etc.

Examples

Calculation of the Faculty

This code must be because the module (line 1) is also called test in the test.erl file. The export directive makes the function fac ( ) callable from outside.

The two lines that begin with fac ( are called clauses. In C jargon, one could compare the use of clauses with the overload. Upon each invocation of a function is successively tried, which Clause match on the given arguments, the first matching clause is used, the last clause ends with, all with previous. ". " ";".

Modules (test). - export ( [ fac / 1, fac_tr / 1] ).   % % % Non- tail recursive version FAC (0) - > 1; FAC (N) -> N * FAC (N -1).   %%% Tail recursive version fac_tr (0, Yet) -> Yet; fac_tr (N, Yet) -> fac_tr (N-1, Yet * N).   % % Auxiliary function fac_tr (N) -> fac_tr (N, 1). Quicksort

% % Quicksort (List) % % Sort a list of items -module ( quicksort ). - export ( [ qsort / 1] ).   qsort ( [ ]) -> []; qsort ( [ Pivot | Rest ]) ->      qsort ( [X | | X <- radical, X < Pivot ] ) [ pivot ] qsort ( [Y | | Y < - Rest, Y> = pivot ] ). In the above example the function qsort recursively invoked until there is nothing available for sorting.

The term

[X | | X <- radical, X < Pivot ] can be interpreted as "select all, X ', where' X 'is an element of, rest ' and 'X' is less than Pivot. '" This has a very convenient mode of treatment result list ( in the literature as a list comprehension refers ). Practically, this means that a list is returned that contains all the elements of group, which are smaller than the pivot.

A small distributed application that runs on two processes Erlang

Modules ( ping_pong ). - export ( [ ping / 0, pong / 0]).   ping () ->      Receiver = spawn ( ping_pong, pong, []),      Receiver! {self (), ping },      receive          pong - >              pong      end.   pong () ->      receive          { Sender, ping } ->              Transmitter! pong      end. Communicating by message passing. The! sends a message that is transmitted asynchronously, that is, the process does not wait until the message has been received.

Mnesia

Entries in the Mnesia database ( see above) are Erlang Records ( Records are syntactic sugar for easier handling of large tuples ), ie tuple { record name, key, field1, field2, field3 } for the pattern. The first field in the tuple must be the table name (in case of Records is the first field in the generated tuple is the name of the record ), the second field is the typical relational database, unique ID in the respective table. example:

% % Shell session % Define Record rd ( table1, { field1, field2 }).   % Mnesia instance create ( structure in the current directory ) Mnesia: CREATE_SCHEMA ( [node ()] ). % Start Mnesia server mnesia: start ( ).   % Define table ' table1 ' with the fields field1 field2 and ( As the Record) mnesia: create_table ( table1, { attributes, record_info ( fields, table1 ) }).   % Define transaction that inserts a record. F = fun () -> Record # table1 = { field1 = hello world, field2 = xyz }, mnesia: write (record ) end. % The same as % F = fun () -> Record = { table1, hello world, xyz }, mnesia: write (record ) end.   % Lead from atomic transaction. mnesia: transaction (F).   % Query transaction. qlc: q () compiles a list comprehensions, qlc: e () executes it. G = fun () -> query = qlc: q ( [X | | X <- mnesia: table ( table1 ) ] ), qlc: e ( query ) end.   % Make of transaction. ListOfTuples is a list of tuples that satisfy the query { atomic, ListOfTuples } = mnesia: transaction (G).   mnesia: stop (). Prominent applications

  • Riak - database
  • CouchDB - database with a focus on document management
  • ejabberd - XMPP server
  • RabbitMQ - AMQP Messaging Server
  • SimpleDB - database
  • WhatsApp - Mobile Messenger
  • Wings 3D - Modeler
  • Yaws - Webserver
  • Yxa - SIP Server
  • Zotonic - CMS / Framework

Commercial use of Erlang

Erlang is one of the few functional programming languages ​​, which are also used in the industry. Among the known users include:

  • Bluetail / Alteon / Nortel ( distributed fault -tolerant e -mail system, SSL accelerator )
  • Cell Point ( Location-based mobile services )
  • Corelatus (SS7 signaling black box ).
  • Ericsson ( AXD301 switch)
  • Facebook ( ejabberd -based chat engine)
  • Finnish Meteorological Institute ( data acquisition and real-time monitoring)
  • GitHub ( RPC )
  • IN Switch Solutions ( ePIN electronic payment system )
  • Mobile Arts ( GSM and UMTS services )
  • Motivity Telecom ( SS7/ISDN gateways)
  • Netkit Solutions ( Networking Monitoring & Operations Support Systems )
  • Process -one ( Commercialized the ejabberd XMPP server)
  • T -Mobile (mobile operator )
  • Telia ( Telecommunications Provider )
  • Vail Systems
  • Tenerife Skunkworks ( online poker server)
  • Clustrx (HPC -A )
  • WhatsApp (Mobile Messenger via XMPP )
  • Tambur.io ( WebSocket Gateway Messaging Software as a Service)
313164
de