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

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

问题描述

大多数主流语言,包括面向对象的编程 (OOP) 语言,例如 C#、Visual Basic、C++ 和 Java,旨在主要支持命令式(过程)编程,而 Haskell/gofer 之类的语言则是纯函数式的.任何人都可以详细说明这两种编程方式之间的区别是什么?

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.

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

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; 

每条语句都会改变程序的状态,从为每个变量赋值到这些值的最终相加.使用五个语句的序列,程序被明确告知如何将数字 5、10 和 15 相加.

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.

对于 OOP 人员或命令式语言:

当您对事物有一组固定的操作并且随着代码的发展,您主要添加新事物时,面向对象的语言是很好的.这可以通过添加实现现有方法的新类并保留现有类来实现.

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.

缺点:

编程方式的选择取决于用户的需求,只有用户不选择合适的方式才会有危害.

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:

  • 向面向对象的程序添加新操作可能需要编辑许多类定义以添加新方法
  • 向函数式程序添加一种新事物可能需要编辑许多函数定义以添加新案例.

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

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