SICP recursive process 和 recursive procedure 的區別

SICP 1.2.1 有一段話:

In contrasting iteration and recursion, we must be careful not to confuse the notion of a recursive process with the notion of a recursive procedure.

摘錄來自: Harold Abelson, Gerald Jay Sussman, Julie Sussman. “Structure and Interpretation of Computer Programs, Second Edition”。 iBooks.

特別強調了不要把 recursive procedurerecursive process 的概念弄混。而要想區分這兩個概念,首先要正確理解 procedureprocess

procedure & process

首先是本節標題 “Procedures and the Processes They Generate”,可以理解爲 procedure 是生成 process 的原料或者元素。

之後又詳細解釋了兩者的關係:

A procedure is a pattern for the local evolution of a computational process.It specifies how each stage of the process is built upon the previous stage. We would like to be able to make statements about the overall, or global, behavior of a process whose local evolution has been specified by a procedure.

即 procedure 和 process 是 local 和 global(overall) 的關係。procedure 關心的是一個表達式樹節點內的計算,而 process 關注的是整個表達式樹的計算。

以斐波那契數列爲例, f(n) = f(n-1) + f(n-2) 這一次加法運算是計算函數的 procedure;但這個 procedure 只是把問題往下遞歸了一層(即文中的 stage),並沒有得出最終值(比如 n=5 時應該返回 5)。而畫出整個遞歸樹後,你看到的這張圖,就是該函數的 process。

recursive

因爲 procedure 和 process 本就是不同層面上的概念,加上 recursive 修飾後也依然指的不是一回事。

recursive procedure指的是:在語法層面上,一個 procedure (直接或間接地)調用了他自己。

recursive process指的是:在邏輯層面上,一個 process 的執行模式 —— 由一連串延遲執行的 procedure 組成。

“This type of process, characterized by a chain of deferred operations, is called a recursive process.”

可以說,recursive process 一定是由 recursive procedure 生成的,但 recursive procedure 卻並不總是生成 recursive process,它還可能生成 iterative process,這種性質被稱爲尾遞歸(tail recursive)。很多語言默認是不打開尾遞歸特性的,因此導致了很多人對 procedure 和 process 區別的困惑。

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