Erlang 代码片段展示了它的好处?

发布于 2024-12-08 02:17:10 字数 285 浏览 1 评论 0原文

我正在向一群对函数式语言缺乏经验的 C/C++ 程序员做一个小型演示。演讲的一部分提到了Erlang,我想给出一个具体的小代码示例。

StackOverflow 上有大量关于如何/在何处使用 Erlang 及其优点的精彩信息。我看到的最常见的优点之一是它只需一点简洁的代码就可以做很多事情,特别是与 C/C++ 相比。

我正在寻找一个很好的 Erlang 代码片段来简单地说明这些类型的好处。尤其是在 Erlang 中只需几行代码就能轻松完成的事情,在 C/C++ 中会复杂得多。

有人有什么有趣的建议吗?

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 terse 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

山有枢 2024-12-15 02:17:11

一位同事建议使用合并排序作为示例:

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

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([]).
手心的温暖 2024-12-15 02:17:11

毕达哥拉斯三元组。获得 30 以下的所有数字组合,其中 3 个数字构成一个直角三角形,就像毕达哥拉斯所说的那样。

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

尝试在 C/C++ 或 Java 中执行此操作,看看是否可以避免 for 循环(如果不超过一个,具体取决于您的技能水平:)

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

生来就爱笑 2024-12-15 02:17:11

阶乘代码片段是我一直用的最好的代码片段,它展示了 erlang 程序有多短

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

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

就这么简单。这个简短的程序不仅说明了 Erlang 程序有多短,而且还说明了:模式匹配< /a>函数子句最后一次调用优化

我总是有一个相同的 C++ 版本,如下:

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

嗯,这可能不是最短的 C++ 版本,但我知道你明白了。

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.

半世晨晓 2024-12-15 02:17:10

查看 示例 4,了解 Erlang 位语法的优秀示例。我确信有许多 c/c++ 开发人员会欣赏语法的简洁性!

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!

时光无声 2024-12-15 02:17:10

我会用一个例子来展示并发是多么容易。

所以基本上要写map-reduce(但永远不要用这个词来向C 程序员描述它)。

您可以首先显示一个播放 Fizz Buzz 的程序,然后继续使其并发。应该可以轻松容纳白板或两页幻灯片。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文