140 likes | 381 Views
Erlang. Paul Ang , Alex Quintero, Jessica Lombardi, Robin Adams. History. 1986 – Developed by Joe Armstrong Worked for Ericsson (Telecommunications company). Created for better telephony Influenced by Plex , Lisp, Prolog, and Parlog but needed concurrency and error handling
E N D
Erlang Paul Ang, Alex Quintero, Jessica Lombardi, Robin Adams
History • 1986 – Developed by Joe Armstrong • Worked for Ericsson (Telecommunications company). • Created for better telephony • Influenced by Plex, Lisp, Prolog, and Parlog but needed concurrency and error handling • After replacing C++ in the AXE-N software in the Ellemtel phone company, Erlang became commercialized • Influenced Clojure, F#, Opa, Reia, Rust, and Scala programming languages • Amazon, Yahoo, Facebook, T-Mobile, Call of Duty
Erlang Development • 1988- First Version • 1989- Second Version- 10 times more efficient than original • 1990- Presented at ISS’90 • 1991- Third Version- interfaces and compiler added • 1993- Commercialized to multiple companies
Programming Basics • “Erlang was designed to run concurrent programs forever” – Joe Armstrong • Functional, not procedural • Recursive, no loops • Concurrent through the use of signal passing • Small functions, but many processes • Benefits: • Fault tolerant • Dynamically upgradable • Works well for parallelism
Data Types • Numbers: Integers and floats • Atoms – similar to constants (true and false are atoms) • Funs – anonymous functions • Sum = fun(X,Y) -> X+Y end. • Sum(5,5) • Tuples – Fixed amount of data. Any item can be accessed in constant time • Lists – Group of data types; varying amount. First item can be accessed in constant time, while other elements can be accessed in O(n) time. • Records – similar to structs in c
Hello World Hello.erl Compile/Run -module(hello). -export([hello_world/0]). hello_world() -> io:fwrite("hello, world\n").
Recursive Functions Recursive.erl Compile/Run -module(recursive). -export([fact/1, qsort/1]). fact(1) -> 1; fact(N) -> N * fact(N - 1). qsort([]) -> []; qsort([H | T]) -> qsort([X || X <- T, X < H]) ++ [H] ++ qsort([X || X <- T, X >= H]).
Guarded Commands Compare.erl Compile/Run -module(guarded). -export([gt/2]). greaterThan(N, M) -> if N > M -> true; N < M -> false; true -> equal end. while(N) when N == 5 -> io:fwrite("~n~B ",[N]), while(N-1); while(N) when N > 0 -> io:fwrite("~B ",[N]), while(N-1); while(_) when true -> done.
Concurrency -module(concurrent). -export([go/1]). go(List) -> Buffer = spawn(fun() -> buff([],5) end), spawn(fun() -> c(Buffer, 1) end), spawn(fun() -> c(Buffer, 2) end), spawn(fun() -> p(List, 1, Buffer) end), spawn(fun() -> p(List, 2, Buffer) end), spawn(fun() -> p(List, 3, Buffer) end), ok. p([], ID, _) -> io:fwrite("P~B Done~n",[ID]); p([H | T], ID, Buffer) -> % Send item to buffer Buffer ! {self(), ID, H}, receive % Check buffer received {_} -> p(T, ID, Buffer) end. c(Buffer,ID) -> Buffer ! {self(), ID}, % Signal buffer to send item receive %Process consumed item {A} -> c(Buffer, ID) % Receive item from buffer end. buff(List, Size) -> io:fwrite("buffer: ~w~n", [List]), receive % Send to the consumer {From, ID} when length(List) > 0 -> [A|B] = List, From ! {A}, % Send message to consumer io:fwrite("C~B Consumed: ~B~n", [ID, A]), buff(B,Size); % Receive from the producer {From, Id, Msg} when length(List) < Size -> From ! {self()}, % Send message to producer io:fwrite("P~B Produced: ~B~n", [Id, Msg]), buff(List ++ [Msg], Size) end.
Concurrency (Output) buffer: [] ok P1 Produced: 1 buffer: [1] C1 Consumed: 1 buffer: [] P2 Produced: 1 buffer: [1] C2 Consumed: 1 buffer: [] P3 Produced: 1 buffer: [1] P1 Produced: 2 buffer: [1,2] C1 Consumed: 1 buffer: [2] P2 Produced: 2 buffer: [2,2] C2 Consumed: 2 buffer: [2] P3 Produced: 2 buffer: [2,2] P1 Produced: 3 buffer: [2,2,3] C1 Consumed: 2 buffer: [2,3] P2 Produced: 3 buffer: [2,3,3] C2 Consumed: 2 buffer: [3,3] P3 Produced: 3 buffer: [3,3,3] P1 Produced: 4 buffer: [3,3,3,4] C1 Consumed: 3 buffer: [3,3,4] P2 Produced: 4 buffer: [3,3,4,4] C2 Consumed: 3 buffer: [3,4,4] P3 Produced: 4 buffer: [3,4,4,4] P1 Produced: 5 buffer: [3,4,4,4,5] C1 Consumed: 3 buffer: [4,4,4,5] P2 Produced: 5 buffer: [4,4,4,5,5] C2 Consumed: 4 buffer: [4,4,5,5] P3 Produced: 5 buffer: [4,4,5,5,5] C1 Consumed: 4 buffer: [4,5,5,5] P1 Produced: 6 P1 Done buffer: [4,5,5,5,6] C2 Consumed: 4 buffer: [5,5,5,6] P2 Produced: 6 P2 Done buffer: [5,5,5,6,6] C1 Consumed: 5 buffer: [5,5,6,6] P3 Produced: 6 P3 Done buffer: [5,5,6,6,6] C2 Consumed: 5 buffer: [5,6,6,6] C1 Consumed: 5 buffer: [6,6,6] C2 Consumed: 6 buffer: [6,6] C1 Consumed: 6 buffer: [6] C2 Consumed: 6 buffer: []