功能和命令式编程语言有什么区别? [英] What is difference between functional and imperative programming languages?

查看:559
本文介绍了功能和命令式编程语言有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数主流语言,包括C#,Visual Basic,C ++和Java等面向对象编程(OOP)语言都是为支持命令式(过程式)编程而设计的,而Haskell / gofer类语言则是纯粹的功能。任何人都可以详细说明这两种编程方式有什么区别吗?



我知道这取决于用户需求选择编程方式,但为什么推荐学习函数式编程语言?


命令式语言使用一系列语句来确定如何达到特定目标。这些语句被称为改变程序的状态,因为每个程序依次执行。



示例:
Java是命令式语言。例如,可以创建一个程序来添加一系列数字:

  int total = 0; 
int number1 = 5;
int number2 = 10;
int number3 = 15;
total = number1 + number2 + number3;

每个语句都会改变程序的状态,从将值分配给每个变量到最终添加值。使用五个语句序列明确告诉程序如何将数字5,10和15加在一起。



功能语言:
功能性编程范例被明确创建,以支持解决问题的纯功能方法。函数式编程是一种声明式编程形式。


纯函数的优点:
实现函数转换作为纯函数的主要原因纯函数是可组合的:即自包含和无状态。这些特点带来了许多好处,包括以下几点:$ b​​ $ b增加了可读性和可维护性。这是因为每个功能的设计都是为了完成一个特定的任务。该函数不依赖于任何外部状态。



更容易的重复开发。由于代码更容易重构,因此对设计的更改通常更容易实现。例如,假设您编写了一个复杂的转换,然后意识到某些代码在转换中会重复多次。如果你通过纯粹的方法重构,你可以随意调用你的纯方法,而不用担心副作用。

更简单的测试和调试。由于纯函数可以更容易地单独测试,因此您可以编写测试代码来调用具有典型值,有效边缘案例和无效边缘案例的纯函数。 strong>对于OOP People或
指令性语言:



面向对象的语言对于事物有固定的一组操作,你的代码会发展,你主要增加新的东西。这可以通过添加实现现有方法的新类来实现,而现有的类是单独存在的。



当你有一组固定的东西时,函数式语言是很好的,代码的发展,你主要增加对现有事物的新操作。这可以通过添加使用现有数据类型进行计算的新函数来实现,而现有的函数是独立的。

缺点:$ b​​
$ b

这取决于用户的要求选择编程方式,所以只有当用户不选择正确的方式时才会造成危害。



当进化发展时错误的方式,你有问题:


  • 添加一个新的操作到面向对象的程序可能需要编辑许多类定义来添加一个新的方法

  • 向功能程序中添加新类型的东西可能需要编辑许多函数定义来添加新的个案。


Most of the mainstream languages, including object-oriented programming (OOP) languages such as C#, Visual Basic, C++, and Java were designed to primarily support imperative (procedural) programming, whereas Haskell/gofer like languages are purely functional. Can anybody elaborate on what is the difference between these two ways of programming?

I know it depends on user requirements to choose the way of programming but why is it recommended to learn functional programming languages?

解决方案

Definition: An imperative language uses a sequence of statements to determine how to reach a certain goal. These statements are said to change the state of the program as each one is executed in turn.

Examples: Java is an imperative language. For example, a program can be created to add a series of numbers:

 int total = 0;
 int number1 = 5;
 int number2 = 10;
 int number3 = 15;
 total = number1 + number2 + number3; 

Each statement changes the state of the program, from assigning values to each variable to the final addition of those values. Using a sequence of five statements the program is explicitly told how to add the numbers 5, 10 and 15 together.

Functional languages: The functional programming paradigm was explicitly created to support a pure functional approach to problem solving. Functional programming is a form of declarative programming.

Advantages of Pure Functions: The primary reason to implement functional transformations as pure functions is that pure functions are composable: that is, self-contained and stateless. These characteristics bring a number of benefits, including the following: Increased readability and maintainability. This is because each function is designed to accomplish a specific task given its arguments. The function does not rely on any external state.

Easier reiterative development. Because the code is easier to refactor, changes to design are often easier to implement. For example, suppose you write a complicated transformation, and then realize that some code is repeated several times in the transformation. If you refactor through a pure method, you can call your pure method at will without worrying about side effects.

Easier testing and debugging. Because pure functions can more easily be tested in isolation, you can write test code that calls the pure function with typical values, valid edge cases, and invalid edge cases.

For OOP People or Imperative languages:

Object-oriented languages are good when you have a fixed set of operations on things and as your code evolves, you primarily add new things. This can be accomplished by adding new classes which implement existing methods and the existing classes are left alone.

Functional languages are good when you have a fixed set of things and as your code evolves, you primarily add new operations on existing things. This can be accomplished by adding new functions which compute with existing data types and the existing functions are left alone.

Cons:

It depends on the user requirements to choose the way of programming, so there is harm only when users don’t choose the proper way.

When evolution goes the wrong way, you have problems:

  • Adding a new operation to an object-oriented program may require editing many class definitions to add a new method
  • Adding a new kind of thing to a functional program may require editing many function definitions to add a new case.

这篇关于功能和命令式编程语言有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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