开发者

Erlang Code Snippet that shows off its benefits?

I'm giving a small presentation to a group of C/C++ programmers who have very little experience with functional languages. Part of the presentation mentions Erlang, and I would like to give a specific small code example.

There is a ton of awesome information on StackOverflow about how/where Erlang is used and its advantages. One of the most common advantages I see is how it can do a lot with just a little ters开发者_Go百科e code, especially compared to C/C++.

I am looking for a good code snippet of Erlang that simply illustrates these types of benefits. Especially something thats easily done in Erlang with few lines, that would be much more complicated in C/C++.

Anyone have any interesting suggestions?


Check out example 4 for an excellent example of Erlang's bit syntax. I'm sure there are a number of c/c++ developers that will appreciate the brevity of the syntax!


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

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

You could start with showing a program that plays Fizz Buzz, and then proceed to make it concurrent. Should easily fit a whiteboard, or two pages of powerpoint.


A Co-worker suggested using Merge-Sort as an example:

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)).

Multi-process version:

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([]).


Pythagorean Triples. Get all number combinations below 30 whereby the 3 numbers make a right angled triangle as it is according to 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 doing that in C/C++ , or Java and see if you will avoid a for loop if not more than one depending on your skill level :)


the factorial code snippet is the best i have always used to show how short erlang programs can be

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

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

As simple as that. That short program illustrates not only how short Erlang programs can be, But also: Pattern Matching, Function Clauses and Last Call Optimization.

I always had a C++ version of the same, below:


#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; } 
}

Well, this may not be the shortest C++ version, but i know you get the idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜