声明式编程范例和过程式编程范例有什么区别?

发布于 2024-08-08 15:57:32 字数 73 浏览 10 评论 0原文

声明式过程式编程范例有什么区别?您能提供一些例子吗?

还存在哪些其他编程范例?

What is the difference between the declarative and procedural programming paradigms? Could you please provide some examples?

What other programming paradigms exist?

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

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

发布评论

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

评论(8

掩于岁月 2024-08-15 15:57:32

命令式

命令式编程范式有几个子范式,例如过程面向对象编程范式。

在命令式编程范例中,您可以以不同的抽象程度逐步描述算法。

支持过程范式的编程语言示例:

  • C(以及大多数其他遗留语言)
  • PHP,主要
  • 在某种意义上,所有主要语言

面向对象

它通常是指表现出类型层次结构的语言,从基类型继承到派生类型的方法和状态,但也包括不寻常的基于原型的 JavaScript。

支持 OO 范式的编程语言示例:

  • Java

声明式

声明式编程范式有多个子范式,例如函数式或逻辑编程范式。

在声明式编程范例中,您描述一个结果或目标,并通过“黑匣子”。与命令式相反。

支持声明式编程范式的编程语言示例:

  • yacc
  • Treetop
  • SQL
  • 正则表达式
  • lex
  • XSLT
  • 标记、troff、CSS、VHDL

函数式

函数式编程强调函数的应用,没有副作用,也没有副作用可变状态。上面的声明性系统展示了函数式编程的某些方面。

支持声明式函数范式的编程语言示例:

  • Haskell
  • OCaml
  • Scheme
  • Erlang
  • F#
  • Scala

Imperative

There are several sub-paradigms of the imperative programming paradigm, such as the procedural or the object-oriented programming paradigms.

In the imperative programming paradigm, you describe the algorithm step-by-step, at various degrees of abstraction.

Examples of programming languages which support the procedural paradigm:

  • C (and most other legacy languages)
  • PHP, mostly
  • In some sense, all major languages

Object-Oriented

It typically refers to languages that exhibit a hierarchy of types that inherit both methods and state from base types to derived types, but also includes the unusual prototype-based JavaScript.

Examples of programming languages which support the OO paradigm:

  • Java

Declarative

There are several sub-paradigms of the declarative programming paradigm, such as the functional or the logic programming paradigms.

In the declarative programming paradigm, you describe a result or a goal, and you get it via a "black box". The opposite of imperative.

Examples of programming languages which support the declarative programming paradigm:

  • yacc
  • Treetop
  • SQL
  • Regular Expressions
  • lex
  • XSLT
  • markup, troff, CSS, VHDL

Functional

Functional programming emphasizes the application of functions without side effects and without mutable state. The declarative systems above exhibit certain aspects of functional programming.

Examples of programming languages which support the declarative functional paradigm:

  • Haskell
  • OCaml
  • Scheme
  • Erlang
  • F#
  • Scala
空宴 2024-08-15 15:57:32

声明式编程是你说出你想要的东西,而不必说出如何去做。使用过程式编程时,您必须指定确切的步骤才能获得结果。

例如,SQL 比过程性更具声明性,因为查询不指定生成结果的步骤。

Declarative programming is where you say what you want without having to say how to do it. With procedural programming, you have to specify exact steps to get the result.

For example, SQL is more declarative than procedural, because the queries don't specify steps to produce the result.

夏末染殇 2024-08-15 15:57:32

让我给你举一个现实世界的例子:我需要一杯茶。

程序式:

  1. 去厨房
  2. 拿糖、牛奶和茶,
  3. 将它们混合,然后在火上加热直至沸腾,
  4. 将其放入杯子中并带给我

声明式:

  1. 给我一杯茶。

在过程语言中,您定义整个过程并提供执行步骤。您只需提供订单并定义流程的服务方式。

在声明性语言中,您只需设置命令或命令,并让它在系统上如何完成该命令。您只需要结果,而不需要深入研究应该如何完成。

Let me give you a real-world example: I need a cup of tea.

Procedural:

  1. Go to kitchen
  2. Get sugar, milk, and tea,
  3. Mix them, and heat over the fire till it boils
  4. Put that in a cup and bring it to me

Declarative:

  1. Get me a cup of tea.

In a procedural language, you define the whole process and provide the steps how to do it. You just provide orders and define how the process will be served.

In a declarative language, you just set the command or order, and let it be on the system how to complete that order. You just need your result without digging into how it should be done.

骷髅 2024-08-15 15:57:32

过程式编程:

在过程式编程中,当程序启动时,它遵循一组指令。这些指令可能会根据某些文件或内存内容而变化,但总体而言,变化不大。程序的输入通常不是来自用户实时输入,而是来自预先收集的数据集。

声明式编程:

在声明式事件驱动编程中,集中于数据体以及程序可以对其执行的可选操作。例如,文字处理器中的每个“事件”是影响数据、文档的任何鼠标或键盘(或文件)更改。它们不需要以任何顺序执行。事件驱动编程采用小程序(事件处理程序)的形式,这些程序都处理一组公共数据,以便每个小程序可以使用相同的数据,即本示例中的文档。

Procedural Programming :

In procedural programming, when the program starts, it follows a set of instructions. The instructions may change based on some file or memory content, but overall, it doesn't vary widely. the input to the program is typically not from user input in real-time, but rather from a pre-gathered set of data.

Declarative Programming:

In Declarative Event driven programming centralizes around a body of data with optional actions the program can take on it. For example, each "event" in a word processor is any mouse or keyboard (or file) changes that affect the data, the document(s). They need not be performed in any order. Event driven programming takes the form of small programs (event handlers) that all work on a common set of data, so that each small program can use the same data, the document in this example.

骑趴 2024-08-15 15:57:32

两种编程语言之间的主要区别是,
在过程式编程中,我们告诉计算机如何解决问题,而在声明性编程中,我们告诉计算机我们想要解决什么问题。

The main difference between two programming languages are,
In procedural programming, we tell the computer how to solve the problem and in declarative programming, we tell the computer what problem we want solved.

不及他 2024-08-15 15:57:32

为了解决 ansible 评论,并可能提供两者之间差异的示例。 Ansible 是程序性的,而像 puppet 或 terraform 这样的东西是声明性的。例如,您创建一个 ansible yaml 文件来部署 10 个 ec2 实例,如下所示:
-ec2:
数量:10
图片:ami-058c6e5b73b074cd2
instance_type: t2.micro

如果您运行该文件两次,您最终会得到 20 个 t2.micro ec2 实例。如果您使用 terraform 等声明性语言编写等效内容并运行两次,则无论运行多少次,都只会运行 10 个 t2.micro 实例。声明性语言指定结束状态。过程语言只是做你告诉它的事情,而不考虑当前或过去的状态。

To address the ansible comment and maybe provide an example between the differences of the two. Ansible is procedural where as something like puppet or terraform are declarative. For example, you create an ansible yaml file to deploy 10 ec2 instances like this:
-ec2:
count: 10
image: ami-058c6e5b73b074cd2
instance_type: t2.micro

If you were to run that file twice, you would end up with 20 t2.micro ec2 instances. If you wrote the equivalent in a declarative language like terraform and ran it twice you would only have 10 t2.micro instances running no matter how many times you ran it. Declarative languages specify end state. Procedural languages just do what you tell it without regard to current or past state.

执笏见 2024-08-15 15:57:32

那么 YAML 是声明式编程语言吗?因为我们定义我们想要的而不是编写实际的逻辑。

我问这个问题是因为如果有人知道 Ansible 这是配置管理工具,它使用 YAML,但它仍然属于过程语言类别。

So YAML is declarative programming language? Because we define what we want instead of writing actual logic.

I am asking this because if anyone knows Ansible which is configuration management tool, it uses YAML but it still falls in procedural language category.

南七夏 2024-08-15 15:57:32

在程序方法中,您对指令进行编码以实现结果。在声明性方法中,您将需要解决的问题定义为解决问题的知识。看看我在两种方法中实现的过程性或声明性方法示例。

正如您在示例中看到的,在声明性方法中,您不需要指示如何解决问题。

In procedural approach you encode your instruction to achieve the result. In the declarative approach you define what needs to be solved as the knowledge of solving the problem. Have a look at Procedural or Declarative approach example I implemented in both approaches.

As you would see in the example, in declarative approach, you do not need to instruct HOW to solve the problem.

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