声明式编程和命令式编程
我正在研究编程的两种主要范式,声明式范式和命令式范式。我很难理解我的教科书和维基百科上的模棱两可的陈述,例如:
声明性: - 专注于计算机要做什么。 - 没有“副作用” - 没有必要的控制流
: - 重点关注计算机应该“如何”做到这一点。 - 如何按照操作顺序来做到这一点
您将如何区分这两种编程范例?如果您能扩展上述陈述,将会非常有帮助。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SQL 是经典的声明性语言:您说“查看此表,并给我满足这些条件的所有行”(在现实生活中您使用联接、选择列表等,但这是相同的基本语句)。正如您上面提到的,该语句告诉计算机您想要什么,而不是如何做。
在内部,数据库系统是用 C 等语言实现的,您的 SQL 查询将被翻译成以下命令式步骤:
这里要注意的关键事项之一是显式控制流: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:
One of the key things to note here is the explicit control flow: while, for, and if. These won't appear in declarative languages.
命令式编程顺序执行可能操纵底层状态的语句。
java中的一些命令式编程:
请注意,如果不按顺序执行上述操作,则会导致空指针异常:
声明式编程没有状态或顺序,只有声明。
这是一个简单的例子 - 这个 xml 片段可以被认为是声明性的:
它不讨论如何构建客户对象,只是声明各个部分。如何解释和执行上述内容取决于编程环境。
Imperative programming sequentially executes statements that may manipulate an underlying state.
Some imperative programming in java:
Note that if you do the above out of order, it would result in a null pointer exception:
Declarative programming doesn't have a state or order, just declarations.
Here is a simple example - this xml snippet could be considered declarative:
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.
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.