声明式编程和命令式编程

发布于 2024-08-06 02:05:29 字数 202 浏览 4 评论 0原文

我正在研究编程的两种主要范式,声明式范式和命令式范式。我很难理解我的教科书和维基百科上的模棱两可的陈述,例如:

声明性: - 专注于计算机要做什么。 - 没有“副作用” - 没有必要的控制流

: - 重点关注计算机应该“如何”做到这一点。 - 如何按照操作顺序来做到这一点

您将如何区分这两种编程范例?如果您能扩展上述陈述,将会非常有帮助。

I am studying two main paradigms of programming, declarative and imperative paradigm. I am having a hard time following the ambiguous statements made on my textbook and wikipedia, such as:

declarative:
- focuses on "what" the computer is to do.
- is free of "side effects"
- without control flow

imperative:
- focuses on "how" the computer should do it.
- how to do it in terms of sequence of actions

how would you differentiate the two programming paradigms? If you could expand on the statements made above, it will be very helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮坠入山谷 2024-08-13 02:05:29

SQL 是经典的声明性语言:您说“查看此表,并给我满足这些条件的所有行”(在现实生活中您使用联接、选择列表等,但这是相同的基本语句)。正如您上面提到的,该语句告诉计算机您想要什么,而不是如何做。

在内部,数据库系统是用 C 等语言实现的,您的 SQL 查询将被翻译成以下命令式步骤:

while (another row to process)
    read row from disk
    for (every test)
        if (test fails)
            continue to next row
    add row to result-set

这里要注意的关键事项之一是显式控制流:while,< em>for 和 if。这些不会出现在声明性语言中。

SQL is the classic declarative language: you say "look at this table, and give me all rows that meet these criteria" (and in real-life you use joins, select lists, whatever, but it's the same basic statement). As you noted above, this statement tells the computer what you want, not how to do it.

Internally, the database system is implemented in a language like C, and your SQL query will be translated into the following imperative steps:

while (another row to process)
    read row from disk
    for (every test)
        if (test fails)
            continue to next row
    add row to result-set

One of the key things to note here is the explicit control flow: while, for, and if. These won't appear in declarative languages.

小情绪 2024-08-13 02:05:29

命令式编程顺序执行可能操纵底层状态的语句。

java中的一些命令式编程:

Customer customer = null;

// first create a customer and have the variable reference it
customer = new Customer(); 
// the state of the customer variable has changed

// set the id on whatever object is *currently* being referenced by the variable
customer.setId(1);
// the state of the Customer object has changed

customer.setFirstName("Bob");
customer.setLastName("McBob");

请注意,如果不按顺序执行上述操作,则会导致空指针异常:

Customer customer = null;
customer.setFirstName("Foo");   // the customer variable is still null at this point
customer = new Customer();   // too late!

声明式编程没有状态或顺序,只有声明。

这是一个简单的例子 - 这个 xml 片段可以被认为是声明性的:

<NewCustomers>
  <Customer>
    <Id>1</Id>
    <FirstName>Bob</FirstName>
    <LastName>McBob</LastName>
  </Customer>
</NewCustomers>

它不讨论如何构建客户对象,只是声明各个部分。如何解释和执行上述内容取决于编程环境。

Imperative programming sequentially executes statements that may manipulate an underlying state.

Some imperative programming in java:

Customer customer = null;

// first create a customer and have the variable reference it
customer = new Customer(); 
// the state of the customer variable has changed

// set the id on whatever object is *currently* being referenced by the variable
customer.setId(1);
// the state of the Customer object has changed

customer.setFirstName("Bob");
customer.setLastName("McBob");

Note that if you do the above out of order, it would result in a null pointer exception:

Customer customer = null;
customer.setFirstName("Foo");   // the customer variable is still null at this point
customer = new Customer();   // too late!

Declarative programming doesn't have a state or order, just declarations.

Here is a simple example - this xml snippet could be considered declarative:

<NewCustomers>
  <Customer>
    <Id>1</Id>
    <FirstName>Bob</FirstName>
    <LastName>McBob</LastName>
  </Customer>
</NewCustomers>

It doesn't talk about how the customer object will get built, just declares the parts. How the above gets interpreted and executed is up to the programming environment.

没有伤那来痛 2024-08-13 02:05:29

HTML 和 Web 浏览器分别是每种范例的很好的例子。 HTML(和 CSS)是声明性语言——您告诉网络浏览器标记什么,但不告诉如何标记。换句话说,像

My title

这样的代码行告诉网络浏览器要显示什么——带有文本“My标题”——但现在如何在屏幕上实际绘制它。

然而,网络浏览器通常是以命令方式编写的。它采用这些声明性标记定义,并实际指示计算机如何将这些元素绘制到屏幕上。

HTML and web browsers are pretty good examples of each paradigm, respectively. HTML (and CSS) are declarative languages -- you tell the web browser what to mark up, but not how to do it. In other words, a line of code like <h1>My title</h1> tells the web browser what to display -- a header tag with the text "My title" -- but now how to actually paint it on the screen.

The web browser, however, is generally written in an imperative manner. It takes those declarative markup definitions and actually instructs the computer how to draw those elements onto the screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文