函数式编程与声明式编程与命令式编程 [英] Functional Programming Vs Declarative Programming Vs Imperative Programming

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

问题描述

我已经太习惯命令式编程了,这是告诉计算机逐步执行程序以获得最终结果的常用方法.另一方面,声明式编程只是传递输入并期望输出,而不说明过程是如何完成的.我感到困惑的是函数式编程.我知道函数式编程是一种编程范式,它将计算视为数学函数的评估并避免状态和可变数据,而不是一种声明性语言.但是,我仍然无法理解它是如何工作的.

I have been too used to Imperative Programming which is the usual way of telling the computer to perform step by step the procedure to get the final result. On the other hand, declarative programming just passes the input and expects the output without stating the procedure how it is done. The one I am confused about is Functional Programming. I know Functional Programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data and is not a type of declarative language. However, I cannot still comprehend how it can work.

让我们举一个执行斐波那契数列的例子.

Let's take an example of executing the Fibonacci numbers.

命令式编程:

#include<stdio.h> 
#include<conio.h> 
main() 
{ 
  int n,i,c,a=0,b=1; 
  printf("Enter Fibonacci series of nth term : "); 
  scanf("%d",&n); 
  printf("%d %d ",a,b); 
  for(i=0;i<=(n-3);i++) 
  { 
    c=a+b; 
    a=b; 
    b=c;    
  } 
  printf("%d ",c);
  getch(); 
} 

声明式编程:

Give the nth number and it will return the value of the nth number

功能程序是如何工作的?

How does the Functional Program work?

如果我的定义有误,请纠正我.请随时发表评论..

Plus do correct me if my definitions are wrong. Please feel free to comment..

推荐答案

您上面的声明式编程示例不是实际程序,因此不是一个很好的示例.

Your example of declarative programming above is not an actual program, so it's not a good example.

主要区别在于命令式和声明式.函数式是一种特殊的声明式.

The main difference is between imperative and declarative. Functional is a particular kind of declarative.

C、C++、Java、Javascript、BASIC、Python、Ruby 和大多数其他编程语言都是必不可少的.通常,如果它有显式循环(for、while、repeat),在每个循环中改变变量,并在每个循环中使用显式赋值操作,那么它是必要的.

C, C++, Java, Javascript, BASIC, Python, Ruby, and most other programming languages are imperative. As a rule, if it has explicit loops (for, while, repeat) that change variables with explicit assignment operations at each loop, then it's imperative.

SQL 和 XSLT 是两个著名的声明式编程示例.HTML 和 CSS 等标记语言也具有声明性,尽管它们通常不足以描述任意算法.

SQL and XSLT are two well-known examples of declarative programming. Markup languages such as HTML and CSS are declarative too, although they are usually not powerful enough to describe arbitrary algorithms.

这是一个计算示例(按性别对收入求和,来自合适的数据源)首先用命令式语言 (Javascript) 编写,然后用声明式语言 (SQL) 编写.

Here is an example computation (summing the income by gender, from a suitable data source) first written in an imperative language (Javascript) and then in a declarative language (SQL).

var income_m = 0, income_f = 0;
for (var i = 0; i < income_list.length; i++) {
    if (income_list[i].gender == 'M')
        income_m += income_list[i].income;
    else
        income_f += income_list[i].income;
}

注意:

  • 显式初始化将包含运行总数的变量;
  • 显式遍历数据,修改控制变量(i)和每次迭代的运行总数;
  • 条件(ifs)仅用于在每次迭代时选择代码路径.
  • explicit initialization of variables that will contain the running totals;
  • explicit loop over the data, modifying the control variable (i) and the running totals at each iteration;
  • conditionals (ifs) are only used to choose the code path at each iteration.
select gender, sum(income)
from income_list
group by gender;

注意:

  • 包含运行总计的内存单元暗示你声明你想要的输出;
  • CPU 需要执行的任何循环(例如,通过income_list 表)由您声明的输出和源数据的结构暗示
  • 条件(例如,SQL 中的case)以功能方式使用,以根据输入值指定输出值, 不选择代码路径.
  • memory cells to contain running totals are implied by the output you declare you want;
  • any loop the CPU will need to perform (eg. over the income_list table) is implied by the output you declare you want and by the structure of the source data;
  • conditionals (eg. case in SQL) are used in a functional way to specify the output value you want based on the input values, not to choose a code path.

正如我上面提到的,SQL 的 case函数式 编程方式的一个很好的例子,它是声明式编程的受限子集,其中指定了所需的计算通过组合函数.

As I mentioned above, SQL's case is a great example of the functional way of programming, which is a restricted subset of Declarative programming in which the desired computation is specified by composing functions.

函数是接受输入并返回输出的事物(例如,casesum()...)

Functions are things that accept inputs and return outputs (eg. case, sum()…)

组合意味着链接两个或多个组合在一起,通过指定如何将一个的输出作为下一个的输入(通常是将一个写入另一个中).最后,整个组合,仍然是本身就是一个大函数,应用于可用的输入以获得所需的输出.

Composition means chaining two or more together by specifying how the output of one is fed as the input to the next (typically by writing one inside the other.) Finally the whole composition, which is still itself a big function, is applied to the available inputs to get the desired output.

在这个片段中,我通过组合函数 sum()case声明我想要的输出.这称为函数式编程:

In this snippet I am declaring the output I want by composing the functions sum() and case. This is called functional programming:

select 
    sum(case when some_flag = 'X' then some_column
        else some_other_column end)
from
    ...

如果两个或多个函数的组合及其对输入数据的应用是给定语言中唯一可用的构造,则该语言被称为纯函数式.在这些语言中,您会注意到完全没有循环、变量赋值和其他典型的命令式语句.

If the composition of two or more functions and their application to the input data are the only constructs available in a given language, that language is said to be purely functional. In those languages you will notice the complete absence of loops, variable assignment and other typically imperative statements.

我建议您观看 Anjana Vakil 的一些关于 Javascript 函数式编程的演讲,以更好地了解它的内容.

I recommend watching some of Anjana Vakil's talks on functional programming in Javascript, to get a better idea of what it's about.

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

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