List comprehensions – Erlangs for elegant-Schleife

In Erlang elegantly and quickly be able to generate a list of numbers much code, it surrounds the special construct of the list comprehension.

They are used mostly in the following cases:

  1. Apply a function to a list
  2. Filtering a list
  3. nested for loops

The syntax of the list comprehensions

[Expression || Generaotor1, Guard11,...GuardN1, Generator2, Guard12,......GeneraotorM, Guard1M,...GuardNM,]

Expression: Specifies the result item in the result list

Generator: similar to a for loop, where each item in the list is iterated: Syntax: Element<-List

Guards: have a filter function, It determines, If an item in the results list ends up

Simple examples of lists comprehensions

[X||X<-[1,2,3,4]].        
[1,2,3,4]
[X||X<-[1,2,3,4], X < 3].
[1,2]
[X||X<-[1,2,3,4], X rem 2 == 0].
[2,4]

more…

Threads in Erlang

With Erlang very easily distributed code can be written, can on different threads running without problems.

Erlang the language of choice is therefore to write distributed applications.

Rules for threads in Erlang

  • Threads cannot access this shared memory space, but communicate with each other via messages only, to prevent collisions.
  • Sending is asynchronous, for synchronous messages self must() be sent and evaluated (self returns the own PID)
  • to communicate between the processes can PID be sent either (elegant), or obtained with whereis() more…

Erlang exercises

Task: 2520 is the smallest number, by any number of 1-10 can be evenly divided. What is the smallest positive number, by all of the numbers 1-20 evenly divisible? It is to be programmed, the Brute Force variant.

Solution:

-module(kleinsterAllesTeiler).
-compile(export_all).

kleinsterAllesTeiler(X) -> kleinsterAllesTeiler(X, 2, X).
kleinsterAllesTeiler(X, Teiler, KleinsteSuperZahl) when (Teiler > X) -> KleinsteSuperZahl;
kleinsterAllesTeiler(X, Teiler, KleinsteSuperZahl)->
    %io:fwrite("X: ~w \t Teiler: ~w \t KleinsteSuperZahl: ~w\n", [X, Teiler, KleinsteSuperZahl]),
    case teilbar(KleinsteSuperZahl, Teiler) of
        true ->kleinsterAllesTeiler(X, Teiler + 1, KleinsteSuperZahl);
        false ->kleinsterAllesTeiler(X, 2, KleinsteSuperZahl + 1)
end.

teilbar(X, Teiler) when (X rem Teiler) == 0 -> true;
teilbar(X, Teiler) -> false.

more…

Erlang Tutorial Windows

For professional development with Erlang, various editors and IDEs available: Overview see item 7.7. Sorry, could not convince me of the Eclipse Plugin and the Netbeans plugin lies not install and Netbeans 7, which is why I use Notepad for developing (see below).

The advantages of the functional programming language Erlang

  1. mathematical proofs can be easily possible
  2. very short source
  3. very suitable for distributed applications
  4. the INT data type is not limited, No numerical effects (Round)
  5. no side effects

Bsp. Applications written in Erlang:

  1. CouchDB
  2. SimpleDB
  3. Yaws – Webserver more…