1.遞迴函式
在函式內部,可以呼叫其他函式。如果一個函式在內部呼叫自身本身,這個函式就是遞迴函式。
舉個例子,我們來計算階乘n! = 1 x 2 x 3 x ... x n
,用函式fact(n)
表示,可以看出:
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)
用遞迴的方式寫出來就是:
def fact(n): if n==1: return 1 return n * fact(n - 1)
上面就是一個遞迴函式。可以試試:
>>> fact(1) 1 >>> fact(5) 120 >>> fact(100) 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L
如果我們計算fact(5)
,可以根據函式定義看到計算過程如下:
===> 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
遞迴函式的優點是定義簡單,邏輯清晰。理論上,所有的遞迴函式都可以寫成迴圈的方式,但迴圈的邏輯不如遞迴清晰。
使用遞迴函式需要注意防止棧溢位。
2.棧溢位
在計算機中,函式呼叫是通過棧(stack)這種資料結構實現的,每當進入一個函式呼叫,棧就會加一層棧幀,每當函式返回,棧就會減一層棧幀。由於棧的大小不是無限的,所以,遞迴呼叫的次數過多,會導致棧溢位。可以試試fact(1000)
:
>>> 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)
引入了乘法表示式,所以就不是尾遞迴了。要改成尾遞迴方式,需要多一點程式碼,主要是要把每一步的乘積傳入到遞迴函式中:
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)
的呼叫如下:
===> 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,可以參考原始碼:
#!/usr/bin/env python2.4 # This program shows off a python decorator( # which implements tail call optimization. It # does this by throwing an exception if it is # it's own grandparent, and catching such # exceptions to recall the stack. import sys class TailRecurseException: def __init__(self, args, kwargs): self.args = args self.kwargs = kwargs def tail_call_optimized(g): """ This function decorates a function with tail call optimization. It does this by throwing an exception if it is it's own grandparent, and catching such exceptions to fake the tail call optimization. This function fails if the decorated function recurses in a non-tail context. """ def func(*args, **kwargs): f = sys._getframe() if f.f_back and f.f_back.f_back \ and f.f_back.f_back.f_code == f.f_code: raise TailRecurseException(args, kwargs) else: while 1: try: return g(*args, **kwargs) except TailRecurseException, e: args = e.args kwargs = e.kwargs func.__doc__ = g.__doc__ return func @tail_call_optimized def factorial(n, acc=1): "calculate a factorial" if n == 0: return acc return factorial(n-1, n*acc) print factorial(10000) # prints a big, big number, # but doesn't hit the recursion limit. @tail_call_optimized def fib(i, current = 0, next = 1): if i == 0: return current else: return fib(i - 1, next, current + next) print fib(10000) # also prints a big number, # but doesn't hit the recursion limit.
現在,只需要使用這個@tail_call_optimized
,就可以順利計算出fact(1000)
:
>>> fact(1000) 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048
0047998861019719605863166687299480855890132382966994459099742450408707375991882362772718873251977950
5950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476
6328898359557354325131853239584630755574091142624174743493475534286465766116677973966688202912073791
4385371958824980812686783837455973174613608537953452422158659320192809087829730843139284440328123155
8611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151
0273418279777047846358681701643650241536913982812648102130927612448963599287051149649754199093422215
6683257208082133318611681155361583654698404670897560290095053761647584772842188967964624494516076535
3408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200
0158885351473316117021039681759215109077880193931781141945452572238655414610628921879602238389714760
8850627686296714667469756291123408243920816015378088989396451826324367161676217916890977991190375403
1274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786
9061172601587835207515162842255402651704833042261439742869330616908979684825901254583271682264580665
2676995865268227280707578139185817888965220816434834482599326604336766017699961283186078838615027946
5955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657
2450144028218852524709351906209290231364932734975655139587205596542287497740114133469627154228458623
7738753823048386568897646192738381490014076731044664025989949022222176590433990188601856652648506179
9702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819
3723886426148396573822911231250241866493531439701374285319266498753372189406942814341185201580141233
4482801505139969429015348307764456909907315243327828826986460278986432113908350621709500259738986355
4277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994
8717012445164612603790293091208890869420285106401821543994571568059418727489980942547421735824010636
7740459574178516082923013535808184009699637252423056085590370062427124341690900415369010593398383577
7939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000