Erlang 代码片段展示了它的好处?
我正在向一群对函数式语言缺乏经验的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一位同事建议使用合并排序作为示例:
http://rosettacode.org/wiki/ Sorting_algorithms/Merge_sort#Erlang
多进程版本:
A Co-worker suggested using Merge-Sort as an example:
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#Erlang
Multi-process version:
毕达哥拉斯三元组。获得 30 以下的所有数字组合,其中 3 个数字构成一个直角三角形,就像毕达哥拉斯所说的那样。
尝试在 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.
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 :)阶乘代码片段是我一直用的最好的代码片段,它展示了 erlang 程序有多短
就这么简单。这个简短的程序不仅说明了 Erlang 程序有多短,而且还说明了:
模式匹配< /a>
、函数子句
和最后一次调用优化
。我总是有一个相同的 C++ 版本,如下:
嗯,这可能不是最短的 C++ 版本,但我知道你明白了。
the factorial code snippet is the best i have always used to show how short erlang programs can be
As simple as that. That short program illustrates not only how short Erlang programs can be, But also:
Pattern Matching
,Function Clauses
andLast Call Optimization
.I always had a C++ version of the same, below:
Well, this may not be the shortest C++ version, but i know you get the idea.
查看 示例 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!
我会用一个例子来展示并发是多么容易。
所以基本上要写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.