Python简单递归

 1 #author F
 2 
 3 #递归 : 自己调用自己
 4 #必须有一个明确的结束条件
 5 #每次进入更深一层递归时 问题规模要比上一层小
 6 #递归效率不高, 递归层次过多会导致栈溢出  函数的调用是通过栈来实现的
 7 def calc(n):
 8     print(n)
 9     return calc(n+1)
10 
11 # calc(0)
12 
13 def mul(n):
14     print(n)
15     if int(n/2) > 0:
16         return mul(int(n/2))
17     print("->", n)
18 
19 mul(10)
20 
21 #函数式编程 : 和函数不是一回事
22 
23 #高阶函数: 一个函数接受令一个函数作为参数
24 
25 def add(a, b, f):
26     return f(a)+f(b)
27 
28 result = add(-3, -4, abs)
29 print(result)

 

posted @ 2017-06-14 11:21  Bird_getUpEarly  阅读(214)  评论(0编辑  收藏  举报