函数式编程有什么好处? [英] What are the benefits of functional programming?

查看:191
本文介绍了函数式编程有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你认为函数式编程的好处是什么?他们今天如何向程序员申请?

函数式编程和OOP之间最大的区别是什么?

解决方案

函数式编程的风格是描述你想要的,而不是如何去获得它。即:而不是用迭代器变量创建一个for循环,并通过一个数组前进到每个单元格做一些事情,你会说相当于这个标签指的是这个数组的一个版本,这个函数已经在所有的元素。

函数式编程将更多基本的编程思想引入编译器,比如列表解析和缓存等。

<函数式编程的最大好处是简洁,因为代码可以更简洁。一个函数式程序不会创建一个迭代器变量作为循环的中心,所以这些和其他类型的开销从您的代码中消除。



另一个主要优点是并发性的,因为编译器正在处理大多数需要手动设置状态变量的操作(比如循环中的迭代器),所以这对于函数式编程来说更容易。



在单处理器环境下也可以看到一些性能优势,具体取决于编写程序的方式,因为大多数功能语言和扩展都支持懒处理评估。在Haskell中,你可以说这个标签代表一个包含所有偶数的数组。这样的数组是无限大的,但是您可以在任何时候请求该数组的第100,000个元素,而无需在数组初始化时知道​​您需要的最大值。这个值只会在你需要的时候计算出来,而不会进一步计算。


What do you think the benefits of functional programming are? And how do they apply to programmers today?

What are the greatest differences between functional programming and OOP?

解决方案

The style of functional programming is to describe what you want, rather than how to get it. ie: instead of creating a for-loop with an iterator variable and marching through an array doing something to each cell, you'd say the equivalent of "this label refers to a version of this array where this function has been done on all the elements."

Functional programming moves more basic programming ideas into the compiler, ideas such as list comprehensions and caching.

The biggest benefit of Functional programming is brevity, because code can be more concise. A functional program doesn't create an iterator variable to be the center of a loop, so this and other kinds of overhead are eliminated from your code.

The other major benefit is concurrency, which is easier to do with functional programming because the compiler is taking care of most of the operations which used to require manually setting up state variables (like the iterator in a loop).

Some performance benefits can be seen in the context of a single-processor as well, depending on the way the program is written, because most functional languages and extensions support lazy evaluation. In Haskell you can say "this label represents an array containing all the even numbers". Such an array is infinitely large, but you can ask for the 100,000th element of that array at any moment without having to know--at array initialization time--just what the largest value is you're going to need. The value will be calculated only when you need it, and no further.

这篇关于函数式编程有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆