| Erlang | |
|---|---|
| Paradigm | multi-paradigm: concurrent, functional |
| Appeared in | 1987 |
| Designed by | Ericsson |
| Developer | Ericsson |
| Typing discipline | dynamic, strong |
| Major implementations | Erlang |
| Influenced | Scala |
| License | Modified MPL |
Erlang is a general-purpose concurrent programming language and runtime system. A programming paradigm is a fundamental style of Computer programming. A multi-paradigm programming language is a Programming language that supports more than one Programming paradigm. Concurrent computing is the concurrent (simultaneous execution of multiple interacting computational tasks In Computer science, functional programming is a Programming paradigm that treats Computation as the evaluation of mathematical functions and Year 1987 ( MCMLXXXVII) was a Common year starting on Thursday (link displays 1987 Gregorian calendar) Ericsson ( Telefonaktiebolaget L M Ericsson) () one of the largest Swedish companies is a leading provider of telecommunication and data communication systems A software developer is a person or organization concerned with facets of the software development process wider than design and coding a somewhat broader scope of Ericsson ( Telefonaktiebolaget L M Ericsson) () one of the largest Swedish companies is a leading provider of telecommunication and data communication systems In Computer science, a type system defines how a Programming language classifies values and expressions into '''types''', how it can In Computer science, a type system defines how a Programming language classifies values and expressions into '''types''', how it can In Computer science and Computer programming, the term strong typing is used to describe those situations where Programming languages specify one or more Implementation is the realization of an application or execution of a Plan, idea Model, Design, Specification, standard, Algorithm Scala ( Scalable Language) is a multi-paradigm Programming language designed to integrate features of Object-oriented programming and A software license (or software licence in commonwealth usage is a Legal instrument governing the usage or redistribution of copyright protected software The Mozilla Public License (MPL is a free and Open source Software license. Concurrent computing is the concurrent (simultaneous execution of multiple interacting computational tasks A programming language is an Artificial language that can be used to write programs which control the behavior of a machine particularly a Computer. In Computer science, runtime or run time describes the operation of a Computer program, the duration of its execution from beginning to termination The sequential subset of Erlang is a functional language, with strict evaluation, single assignment, and dynamic typing. In Computer science, functional programming is a Programming paradigm that treats Computation as the evaluation of mathematical functions and Eager evaluation or strict evaluation is the Evaluation strategy in most traditional Programming languages In eager evaluation an expression Single assignment is used to describe a programming language or representation in which one cannot bind a value to a name if a value has already been bound to that In Computer science, a type system defines how a Programming language classifies values and expressions into '''types''', how it can For concurrency it follows the Actor model. In Computer science, the Actor model is a mathematical model of Concurrent computation that treats "actors" as the universal primitives of concurrent It was designed by Ericsson to support distributed, fault-tolerant, soft-real-time, non-stop applications. Ericsson ( Telefonaktiebolaget L M Ericsson) () one of the largest Swedish companies is a leading provider of telecommunication and data communication systems This article contains specific implementations of fault tolerant systems In Computer science, real-time computing (RTC is the study of hardware and software systems that are subject to a "real-time constraint"—i It supports hot swapping so code can be changed without stopping a system. Hot swapping and hot plugging are terms used to separately describe the functions of replacing system components hot swapping describes changing components like fans and [1] Erlang was originally a proprietary language within Ericsson, but was released as open source in 1998. Open source is a development methodology which offers practical accessibility to a product's source (goods and knowledge The Ericsson implementation primarily runs interpreted virtual machine code, but it also includes a native code compiler (supported on most but not all platforms), developed by the High Performance Erlang Project (HiPE)[2] at Uppsala University. In Computer science, an interpreter normally means a Computer program that executes, i Uppsala University ( Swedish Uppsala universitet) is a world-class research University in Uppsala, Sweden. It also now supports interpretation via script as of r11b-4.
Creating and managing processes is trivial in Erlang, whereas threads are considered a complicated and error-prone topic in most languages. A thread in Computer science is short for a thread of execution. Though all concurrency is explicit in Erlang, processes communicate using message passing instead of shared variables, which removes the need for locks. In Computer science, message passing is a form of communication used in Parallel computing, Object-oriented programming, and Interprocess communication In Computer science, a lock is a synchronization mechanism for enforcing limits on access to a resource in an environment where there are many threads of
Erlang is named after A. K. Erlang. Agner Krarup Erlang ( January 1, 1878 &ndash February 3, 1929) was a Danish Mathematician, Statistician and It is sometimes thought that its name is an abbreviation of Ericsson Language, owing to its origin inside Ericsson. According to Bjarne Däcker, who headed the Computer Science Lab at the time, this duality is intentional. [1]
Contents |
Code looks like this:
-module(fact). -export([fac/1]). fac(0) -> 1; fac(N) -> N * fac(N-1).
Below is an implementation of a Quicksort algorithm.
%% quicksort:qsort(List) %% Sort a list of items -module(quicksort). -export([qsort/1]). qsort([]) -> []; qsort([Pivot|Rest]) -> qsort([ X || X <- Rest, X < Pivot]) ++ [Pivot] ++ qsort([ Y || Y <- Rest, Y >= Pivot]).
The above example recursively invokes the function qsort until nothing remains to be sorted. The expression [ X || X <- Rest, X < Pivot] means “Choose all X where X is a member of Rest and X is less than Pivot”, resulting in a very easy way of handling lists. Since you can evaluate any boolean expression between two different datatypes, the evaluation is straightforward: for example, 1 < a will return true.
A compare function can be used, however, if the order on which Erlang bases its return value (true or false) needs to be changed. If, for example, we want an ordered list where a < 1 evaluates true.
The following code would sort lists according to length:
-module(listsort). -export([by_length/1]). by_length(Lists) -> F = fun(A,B) when is_list(A), is_list(B) -> length(A) < length(B) end, qsort(Lists, F). qsort([], _)-> []; qsort([Pivot|Rest], Smaller) -> qsort([ X || X <- Rest, Smaller(X,Pivot)], Smaller) ++ [Pivot] ++ qsort([ Y ||Y <- Rest, not(Smaller(Y, Pivot))], Smaller).
Erlang's main strength is support for concurrency. In Computer science, concurrency is a properties of system in which several Computational processes are executing at the same time and potentially interacting It has a small but powerful set of primitives to create processes and communicate between them. Processes are the primary means to structure an Erlang application. Erlang processes are neither operating system processes nor operating system threads, but lightweight processes somewhat similar to Java's original “green threads” (the Java Virtual Machine now uses native threads). In Computer programming, Green threads are threads that are scheduled by a Virtual Machine (VM instead of natively by the underlying Operating system Like operating system processes (and unlike green threads and operating system threads) they have no shared state between them. The estimated minimal overhead for each is 300 words (4 bytes per word on 32-bit platforms, 8 bytes per word on 64-bit platforms), so many of them can be created without degrading performance (a benchmark with 20 million processes was tried[3]). Erlang has supported symmetric multiprocessing since release R11B of May 2006. In Computing, symmetric multiprocessing or SMP involves a Multiprocessor computer-architecture where two or more identical processors can connect to a single
Process communication is done via a share-nothing asynchronous message-passing system: every process has a “mailbox”, a queue of messages sent by other processes, that are not yet consumed. A queue (pronounced /kjuː/ is a particular kind of collection in which the entities in the collection are kept in order and the principal (or only operations on the collection A process uses the receive primitive to retrieve messages that match desired patterns. A message-handling routine tests messages in turn against each pattern, until one of them matches. When the message is consumed (removed from the mailbox) the process resumes execution. A message may comprise any Erlang structure, including primitives (integers, floats, characters, atoms), tuples, lists, and functions.
Code examples:
% create process and call the function web:start_server(Port, MaxConnections)
ServerProcess = spawn (web, start_server, [Port, MaxConnections]),
% create a remote process and call the function web:start_server(Port, MaxConnections) on machine RemoteNode
RemoteProcess = spawn(RemoteNode, web, start_server, [Port, MaxConnections]),
% send the {pause, 10} message (a tuple with an atom "pause" and a number "10") to ServerProcess (asynchronously)
ServerProcess ! {pause, 10},
% receive messages sent to this process
receive
a_message -> do_something;
{data, DataContent} -> handle(DataContent);
{hello, Text} -> io:format("Got hello message: ~s", [Text]);
{goodbye, Text} -> io:format("Got goodbye message: ~s", [Text])
end.
As the example shows, there is built-in support for distributed processes. Processes may be created on remote nodes, and communication with them is transparent (i. e. the communication with remote processes is done exactly as the communication with local processes).
Concurrency supports the primary method of error-handling in Erlang. When a process crashes, it neatly exits and sends a message to the controlling process which can take action. This way of error handling may increase maintainability and reduce complexity of code.
Code is loaded and managed as "module" units, the module is a compilation unit. See C programming language for translation unit in the context of Computer programming. The system can keep two versions of a module in memory at the same time, and processes can concurrently run code from each. The versions are referred to the "new" and the "old" version. A process will not move into the new version until it makes an external call to its module.
An example of the mechanism of hot code loading:
%% A process whose only job is to keep a counter.
%% First version
-module(counter).
-export([start/0, codeswitch/1]).
start() -> loop(0).
loop(Sum) ->
receive
{increment, Count} ->
loop(Sum+Count);
{counter, Pid} ->
Pid ! {counter, Sum},
loop(Sum);
code_switch ->
?MODULE:codeswitch(Sum)
end.
codeswitch(Sum) -> loop(Sum).
For the second version, we add the possibility to reset the count to zero.
%% Second version
-module(counter).
-export([start/0, codeswitch/1]).
start() -> loop(0).
loop(Sum) ->
receive
{increment, Count} ->
loop(Sum+Count);
reset ->
loop(0);
{counter, Pid} ->
Pid ! {counter, Sum},
loop(Sum);
code_switch ->
?MODULE:codeswitch(Sum)
end.
codeswitch(Sum) -> loop(Sum).
Only when receiving a message consisting of the atom 'code_switch' will the loop execute an external call to codeswitch/1 (?MODULE is a preprocessor macro for the current module). If there is a new version of the "counter" module in memory, then its codeswitch/1 function will be called. The practice of having a specific entry-point into a new version allows the programmer to transform state to what is required in the newer version. In our example we keep the state as an integer.
In practice systems are built up using design principles from the Open Telecom Platform which leads to more code upgradable designs. Successful hot code loading is a tricky subject, code needs to be written to make use of Erlangs facilities.
Erlang was released by Ericsson as open-source to ensure its independence from a single vendor and to increase awareness of the language. Distribution of the language together with libraries and the real-time distributed database Mnesia is the Open Telecom Platform (OTP). Ericsson and a few other companies offer commercial support for Erlang.
Since it was released as open source in 1998 it has been used by several companies world-wide, including Nortel and T-Mobile[4], although it has not become a widespread programming language. Year 1998 ( MCMXCVIII) was a Common year starting on Thursday (link will display full 1998 Gregorian calendar) Nortel Networks Corporation () formerly known as Northern Telecom Limited and sometimes known simply as Nortel, is a multinational Telecommunications T-Mobile is a Mobile network operator headquartered in Bonn, Germany.
As of 2008, Erlang is under active development with regular releases. 2008 ( MMVIII) is the current year in accordance with the Gregorian calendar, a Leap year that started on Tuesday of the Common It is available for several Unix-like operating systems and Microsoft Windows. A Unix-like (sometimes shortened to *nix) Operating system is one that behaves in a manner similar to a Unix system while not necessarily conforming Microsoft Windows is a series of Software Operating systems and Graphical user interfaces produced by Microsoft.