Erlang Code Snippet showing off the benefits?

I give a small presentation to a group of C / C ++ programmers who have very little experience working with functional languages. Erlang is mentioned in the presentation part, and I would like to give an example of a small code.

There is a lot of awesome StackOverflow information on how / where Erlang is used and its benefits. One of the most common benefits that I see is how it can do a lot with just a little short code, especially when compared to C / C ++.

I am looking for a good piece of Erlang code that simply illustrates these benefits. Especially something that is easy to do in Erlang with multiple lines, which would be much more complicated in C / C ++.

Anyone have any interesting suggestions?

+5
source share
5 answers

Check Example 4 for a great example of the Erlang bit syntax. I'm sure there are several c / C ++ developers who will appreciate the brevity of the syntax!

+4
source

I would use an example that shows how easy it is to do concurrency.

So basically write map-reduce (but never use this word to describe it to a C programmer).

You can start by showing a program that plays Fizz Buzz , and then move on to it at the same time. It should easily go to the board or to two pages of powerpoint.

+3
source

Merge-Sort :

http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#Erlang

mergeSort(L) when length(L) == 1 -> L;
mergeSort(L) when length(L) > 1 ->
    {L1, L2} = lists:split(length(L) div 2, L),
    lists:merge(mergeSort(L1), mergeSort(L2)).

:

pMergeSort(L) when length(L) == 1 -> L;
pMergeSort(L) when length(L) > 1 ->
    {L1, L2} = lists:split(length(L) div 2, L),
    spawn(mergesort, pMergeSort2, [L1, self()]),
    spawn(mergesort, pMergeSort2, [L2, self()]),
    mergeResults([]).

pMergeSort2(L, Parent) when length(L) == 1 -> Parent ! L;
pMergeSort2(L, Parent) when length(L) > 1 ->
    {L1, L2} = lists:split(length(L) div 2, L),
    spawn(mergesort, pMergeSort2, [L1, self()]),
    spawn(mergesort, pMergeSort2, [L2, self()]),
    Parent ! mergeResults([]).
+3

, , , erlang

-module(factorial).
-export([calculate/1]).

calculate(0) -> 1;
calculate(N) -> N * calculate(N -1).

. , Erlang , : Pattern Matching, Function Clauses Last Call Optimization.

++, :

#include<iostream.h>
#include<conio.h>

long factorial(unsigned int a);

void main() {
    unsigned int a;
    long fac;
    .....
    .....
    return factorial(a); 
}

long factorial(unsigned int x) {
        long fac=1;
        if(x == 0) {return 1;}
        else {
                while(x > 0) {
                    fac *= x;
                    x -= 1 ;
                }
        return fac; } 
}

, ++, , .

+2

Pythagorean trinity. Get all the numerical combinations below 30, resulting in 3 numbers forming a right triangle, as described in Pythagoras.

[{X,Y,Z} || X <- lists:seq(1,30),
            Y <- lists:seq(1,30),
            Z <- lists:seq(1,30), ((X * X) + (Y * Y)) == (Z * Z)].

Try this in C / C ++ or Java and see if you can avoid for loop, if not more than one, depending on your skill level :)

+2
source

All Articles