天天看點

[轉]程式設計語言的主要類型,聲明式程式設計,指令式程式設計()和函數式程式設計的差別

程式設計語言的主要類型

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+2) * 3 / 4
           

指令式程式設計可能會這樣寫

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

函數式程式設計如下寫法

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

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

我們想讓一個數組裡的數值翻倍。

我們用指令式程式設計風格實作,像下面這樣:

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]
           

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

而使用聲明式程式設計方法,我們可以用 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]
           

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

參考文章

1.聲明式程式設計和指令式程式設計的比較

2.阮一峰的函數程式設計初探文章