声明式编程与(控制)算法

In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an imperative programming language from a declarative programming language.

A declarative language declares a set of rules about what outputs should result from which inputs (eg. if you have A, then the result is B). An engine will apply these rules to inputs, and give an output.

Declarative programming : is a programming paradigm that expresses the logic of a computation(What do) without describing its control flow(How do). Some well-known examples of declarative domain specific languages (DSLs) include CSS, regular expressions, and a subset of SQL (SELECT queries, for example) Many markup languages such as HTML, MXML, XAML, XSLT... are often declarative. The declarative programming try to blur the distinction between a program as a set of instructions and a program as an assertion about the desired answer.

Functional programming : is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

I liked an explanation from a Cambridge course + their examples:

  • Declarative - specify what to do, not how to do it
    • E.g.: HTML describes what should appear on a web page, not how it should be drawn on the screen
  • Imperative - specify both what and how
    • int x; - what (declarative)
    • x=x+1; - how

In computer science, declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow.

that expresses the logic of a computation without describing its control flow

https://stackoverflow.com/questions/1784664/what-is-the-difference-between-declarative-and-imperative-programming

首先从第一个差异声明范式开始,声明式编程的重点是应该做什么事,而不是如何去做(后者是命令式编程)。命令式语言往往集中在一个程序的方法,明确如何实现具体操作及改变程序的状态,而声明式编程表达操作的逻辑(不要着眼于实现)。

声明式编程透过函数、推论规则或项重写(term-rewriting)规则,来描述变量之间的关系。它的语言运行器编译器解释器)采用了一个固定的算法,以从这些关系产生结果。

原文地址:https://www.cnblogs.com/feng9exe/p/10191424.html