我一直在网上搜索声明式编程和命令式编程的定义,希望能给我一些启发。然而,我发现在一些资源中使用的语言令人生畏——例如在维基百科。 有没有人可以给我举一个现实世界的例子,让我对这个主题有一些了解(也许是c#)?


当前回答

声明性程序只是它的一些或多或少的“通用”命令式实现/虚拟机的数据。

pluses: specifying just a data, in some hardcoded (and checked) format, is simpler and less error-prone than specifying variant of some imperative algorithm directly. some complex specifications just cant be written directly, only in some DSL form. best and freq used in DSLs data structures is sets and tables. because you not have dependencies between elements/rows. and when you havent dependencies you have freedom to modify and ease of support. (compare for example modules with classes - with modules you happy and with classes you have fragile base class problem) all goods of declarativeness and DSL follows immediately from benefits of that data structures (tables and sets). another plus - you can change implementation of declarative language vm, if DSL is more-or-less abstract (well designed). make parallel implementation, for example. or port it to other os etc. all good specifed modular isolating interfaces or protocols gives you such freedom and easyness of support.

缺点: 你猜对了。一般的(通过DSL参数化的)命令式算法/虚拟机实现可能比特定的算法慢和/或占用内存。在某些情况下。 如果这种情况很罕见,那就忘了它吧,让它慢慢来。如果这种情况经常发生,你总是可以扩展你的DSL/vm。某个能减缓其他案件的地方,当然…

P.S.框架介于DSL和命令式之间。作为所有的折中方案……他们结合了缺点,而不是优点。它们不是那么安全,也不是那么快:)看看万事通haskell——它介于强大的简单ML和灵活的元程序Prolog之间……真是个怪物。你可以把Prolog看作一个Haskell,它只包含布尔函数/谓词。它对Haskell的灵活性是多么简单……

其他回答

命令式编程是显式地告诉计算机做什么,以及如何做,比如指定顺序等

C#:

for (int i = 0; i < 10; i++)
{
    System.Console.WriteLine("Hello World!");
}

说明性是指你告诉计算机做什么,而不是如何做。Datalog / Prolog是在这方面首先想到的语言。基本上所有东西都是声明性的。你不能保证秩序。

c#是一种更命令式的编程语言,但某些c#特性更具有声明性,如Linq

dynamic foo = from c in someCollection
           let x = someValue * 2
           where c.SomeProperty < x
           select new {c.SomeProperty, c.OtherProperty};

同样的东西也可以写成命令式:

dynamic foo = SomeCollection.Where
     (
          c => c.SomeProperty < (SomeValue * 2)
     )
     .Select
     (
          c => new {c.SomeProperty, c.OtherProperty}
     )

(例子来自维基百科Linq)

在计算机科学中,声明式编程是一种编程范式,它在不描述控制流的情况下表达计算逻辑。

从http://en.wikipedia.org/wiki/Declarative_programming

简而言之,声明式语言更简单,因为它缺乏控制流(循环、if语句等)的复杂性。

一个很好的比较是ASP。网络“后台代码”模型。你们有陈述句。'ASPX '文件,然后是'ASPX. cs '代码文件。我经常发现,如果我可以在脚本的声明性部分完成我所需要的一切,那么更多的人可以理解所做的事情。

Imperative programming requires developers to define step by step how code should be executed. To give directions in an imperative fashion, you say, “Go to 1st Street, turn left onto Main, drive two blocks, turn right onto Maple, and stop at the third house on the left.” The declarative version might sound something like this: “Drive to Sue’s house.” One says how to do something; the other says what needs to be done. The declarative style has two advantages over the imperative style: It does not force the traveler to memorize a long set of instructions. It allows the traveler to optimize the route when possible.

卡尔弗特,C库尔卡尼,D(2009)。LINQ至关重要。艾迪森·韦斯利48岁

我将添加另一个在声明式/命令式编程讨论中很少出现的示例:用户界面!

在c#中,您可以使用各种技术构建UI。

在命令式端,你可以使用DirectX或OpenGL命令式地绘制按钮、复选框等。一行一行的(或者实际上是一个三角形一个三角形的)。如何绘制用户界面由您决定。

在声明端,有WPF。您只需编写一些XML(是的,是的,技术上是“XAML”),框架就会为您完成工作。你说用户界面是什么样的。如何做到这一点取决于系统。

总之,这是另一件值得思考的事情。仅仅因为一种语言是声明式或命令式的,并不意味着它没有另一种语言的某些特性。

此外,声明式编程的一个好处是,通过阅读代码通常更容易理解目的,而命令式编程则可以更好地控制执行。

要点是:

说明性->你想做什么

命令式->你想怎么做

我只是想知道为什么没有人提到Attribute类是c#中的声明性编程工具。本页最流行的回答是将LINQ作为一种声明性编程工具。

根据维基百科

常见的声明性语言包括数据库查询语言 (如SQL, XQuery),正则表达式,逻辑编程, 功能编程和配置管理系统。

所以LINQ作为一个函数式语法,绝对是一个声明性的方法,但是c#中的Attribute类作为一个配置工具,也是声明性的。这里有一个很好的起点来阅读更多关于它的内容:c#属性编程的快速概述