本文的最重要的收穫在於:尾遞歸是指,在函數返回的時候,調用自身自己,而且,return語句不能包含表達式。編程
在函數內部,能夠調用其餘函數。若是一個函數在內部調用自身自己,這個函數就是遞歸函數。數據結構
舉個例子,咱們來計算階乘n! = 1 x 2 x 3 x ... x n,用函數fact(n)表示,能夠看出:編程語言
1
|
fact(n)
=
n!
=
1
x
2
x
3
x ... x (n
-
1
) x n
=
(n
-
1
)! x n
=
fact(n
-
1
) x n
|
因此,fact(n)能夠表示爲n x fact(n-1),只有n=1時須要特殊處理。函數
因而,fact(n)用遞歸的方式寫出來就是:優化
1
2
3
4
|
def
fact(n):
if
n
=
=
1
:
return
1
return
n
*
fact(n
-
1
)
|
上面就是一個遞歸函數。能夠試試:spa
1
2
3
4
5
6
|
>>> fact(
1
)
1
>>> fact(
5
)
120
>>> fact(
100
)
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L
|
若是咱們計算fact(5),能夠根據函數定義看到計算過程以下:.net
1
2
3
4
5
6
7
8
9
10
|
=
=
=
> fact(
5
)
=
=
=
>
5
*
fact(
4
)
=
=
=
>
5
*
(
4
*
fact(
3
))
=
=
=
>
5
*
(
4
*
(
3
*
fact(
2
)))
=
=
=
>
5
*
(
4
*
(
3
*
(
2
*
fact(
1
))))
=
=
=
>
5
*
(
4
*
(
3
*
(
2
*
1
)))
=
=
=
>
5
*
(
4
*
(
3
*
2
))
=
=
=
>
5
*
(
4
*
6
)
=
=
=
>
5
*
24
=
=
=
>
120
|
遞歸函數的優勢是定義簡單,邏輯清晰。理論上,全部的遞歸函數均可以寫成循環的方式,但循環的邏輯不如遞歸清晰。code
使用遞歸函數須要注意防止棧溢出。在計算機中,函數調用是經過棧(stack)這種數據結構實現的,每當進入一個函數調用,棧就會加一層棧幀,每當函數返回,棧就會減一層棧幀。因爲棧的大小不是無限的,因此,遞歸調用的次數過多,會致使棧溢出。能夠試試fact(1000):htm
1
2
3
4
5
6
7
|
>>> fact(
1000
)
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
File
"<stdin>"
, line
4
,
in
fact
...
File
"<stdin>"
, line
4
,
in
fact
RuntimeError: maximum recursion depth exceeded
|
解決遞歸調用棧溢出的方法是經過尾遞歸優化,事實上尾遞歸和循環的效果是同樣的,因此,把循環當作是一種特殊的尾遞歸函數也是能夠的。遞歸
尾遞歸是指,在函數返回的時候,調用自身自己,而且,return語句不能包含表達式。這樣,編譯器或者解釋器就能夠把尾遞歸作優化,使遞歸自己不管調用多少次,都只佔用一個棧幀,不會出現棧溢出的狀況。
上面的fact(n)函數因爲return n * fact(n - 1)引入了乘法表達式,因此就不是尾遞歸了。要改爲尾遞歸方式,須要多一點代碼,主要是要把每一步的乘積傳入到遞歸函數中:
1
2
3
4
5
6
7
|
def
fact(n):
return
fact_iter(
1
,
1
, n)
def
fact_iter(product, count,
max
):
if
count >
max
:
return
product
return
fact_iter(product
*
count, count
+
1
,
max
)
|
能夠看到,return fact_iter(product * count, count + 1, max)僅返回遞歸函數自己,product * count和count + 1在函數調用前就會被計算,不影響函數調用。
fact(5)對應的fact_iter(1, 1, 5)的調用以下:
1
2
3
4
5
6
7
|
=
=
=
> fact_iter(
1
,
1
,
5
)
=
=
=
> fact_iter(
1
,
2
,
5
)
=
=
=
> fact_iter(
2
,
3
,
5
)
=
=
=
> fact_iter(
6
,
4
,
5
)
=
=
=
> fact_iter(
24
,
5
,
5
)
=
=
=
> fact_iter(
120
,
6
,
5
)
=
=
=
>
120
|
尾遞歸調用時,若是作了優化,棧不會增加,所以,不管多少次調用也不會致使棧溢出。
遺憾的是,大多數編程語言沒有針對尾遞歸作優化,Python解釋器也沒有作優化,因此,即便把上面的fact(n)函數改爲尾遞歸方式,也會致使棧溢出。
有一個針對尾遞歸優化的decorator,能夠參考源碼:
http://code.activestate.com/recipes/474088-tail-call-optimization-decorator/
咱們後面會講到如何編寫decorator。如今,只須要使用這個@tail_call_optimized,就能夠順利計算出fact(1000):
1
2
|
>>> fact(
1000
)
402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
小結
使用遞歸函數的優勢是邏輯簡單清晰,缺點是過深的調用會致使棧溢出。
針對尾遞歸優化的語言能夠經過尾遞歸防止棧溢出。尾遞歸事實上和循環是等價的,沒有循環語句的編程語言只能經過尾遞歸實現循環。
Python標準的解釋器沒有針對尾遞歸作優化,任何遞歸函數都存在棧溢出的問題。