編程語言的主要類型,聲明式編程,命令式編程()和函數式編程的區別

編程語言的主要類型

Common programming paradigms include imperative which allows side effects, functional which disallows side effects, declarative which does not state the order in which operations execute
(翻譯:常見的編程語言類型包括允許有副作用的命令式編程,不允許副作用的函數式編程和不描述操作執行順序的聲明式編程)

A programming paradigm is a fundamental style of computer programming. There are four main paradigms: imperative, declarative, functional (which is considered a subset of the declarative paradigm) and object-oriented.

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.

Imperative programming : is a programming paradigm that describes computation in terms of statements that change a program state. The declarative programs can be dually viewed as programming commands or mathematical assertions.

Functional programming : is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. In a pure functional language, such as Haskell, all functions are without side effects, and state changes are only represented as functions that transform the state.

( 出處:維基百科)

翻譯總結:

編程語言主要有四種類型

  1. 聲明式編程:專注於”做什麼”而不是”如何去做”。在更高層面寫代碼,更關心的是目標,而不是底層算法實現的過程。
    ex: css, 正則表達式,sql 語句,html, xml…
  2. 命令式編程(過程式編程) : 專注於”如何去做”,這樣不管”做什麼”,都會按照你的命令去做。解決某一問題的具體算法實現。
  3. 函數式編程:把運算過程儘量寫成一系列嵌套的函數調用。
    函數式編程強調沒有”副作用”,意味着函數要保持獨立,所有功能就是返回一個新的值,沒有其他行爲,尤其是不得修改外部變量的值。
    所謂”副作用”(side effect),指的是函數內部與外部互動(最典型的情況,就是修改全局變量的值),產生運算以外的其他結果。(詳細瞭解函數式編程請看阮一峯的函數編程初探文章)

舉個簡單的例子,瞭解三者區別
1.現有這樣一個數學表達式

(1+2) * 3 / 4
  • 1
  • 2

命令式編程可能會這樣寫

var a = 1 + 2;
var b = a * 3;
var c = b / 4;
  • 1
  • 2
  • 3
  • 4

函數式編程如下寫法

divide(multiply(add(1, 2), 3), 4)
  • 1
  • 2

函數式編程是聲明式的一種類型,聲明式強調目標而不是具體過程。

  1. 我們想讓一個數組裏的數值翻倍。

我們用命令式編程風格實現,像下面這樣:

var numbers = [1,2,3,4,5]
var doubled = []
for(var i = 0; i < numbers.length; i++) {
  var newNumber = numbers[i] * 2
  doubled.push (newNumber)
}
console.log (doubled) //=> [2,4,6,8,10]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

我們直接遍歷整個數組,取出每個元素,乘以二,然後把翻倍後的值放入新數組,每次都要操作這個雙倍數組,直到計算完所有元素。

而使用聲明式編程方法,我們可以用 Array.map 函數,像下面這樣:

var numbers = [1,2,3,4,5]
var doubled = numbers.map (function (n) {
  return n * 2
})
console.log (doubled) //=> [2,4,6,8,10]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

map利用當前的數組創建了一個新數組,新數組裏的每個元素都是經過了傳入map的函數(這裏是function (n) { return n*2 })的處理。
  map函數所做的事情是將直接遍歷整個數組的過程歸納抽離出來,讓我們專注於描述我們想要的是什麼(what)。注意,我們傳入map的是一個純函數;它不具有任何副作用(不會改變外部狀態),它只是接收一個數字,返回乘以二後的值。

參考文章
1.聲明式編程和命令式編程的比較
2.阮一峯的函數編程初探文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章