Structure and Interpretation of Computer Programs sec_1.1.8
#1.1.8 Procedures as Black-Box Abstractions(ブラックボックス抽象化の手続き) #Internal definitions and block structure(内在的な定義とブロック構造) #平方根の例 def square(x): return x * x def average(x, y): return (x + y) / 2 def sqrt(x): def good_enough(guess): if abs(square(guess) - x) < 0.001: return True def improve(guess): return average(guess, x / guess) def sqrt_iter (guess): if good_enough(guess): return guess else: return sqrt_iter(improve(guess)) return sqrt_iter(1.0) print sqrt(2) #1.41421568627