# 高阶函数、递归函数和内置函数

``````#函数名可以进行赋值，可以作为函数参数，可以作为返回值
#高阶函数：允许导入函数作为参数导入或者返回值为函数
def f(n):
return n*n

def fun(a,b,fun1):
an1=fun1(a)+fun1(b)
return (an1)

print(fun(1,2,f))

def sqrt(n):
return n*n
def function(a,b,function1):
print(function(1,8,sqrt))
# 65``````
``````#递归函数
#递归函数特点：自己调用自己，结束判断
#递归可以写的程序，循环都可以做，递归效率在很多时候很低
def recursion(n):
if n==1:
return 1

return n*recursion(n-1)

print(recursion(5))
# 120``````
``````#斐波那切数列
def fibo_seq(n):
if n==1:
return 0
elif n==2:
return 1
else:
return fibo_seq(n-1)+fibo_seq(n-2)

print(fibo_seq(10))``````

``````print(any([1]))
print(eval("1+2*3"))  #eval有计算器功能
#eval()函数能把字符串类型成其他类型执行，比如转换成列表，元组和字典``````
``````#filter函数
list1=[1,2,3,4]

def fun1(l):
if l!=1:
return l

ret=filter(fun1,list1)
# ret变成迭代器
print(ret)
# <filter object at 0x000000911CD9B978>
print(list(ret))
# [2, 3, 4]``````
``````#map函数
def fun2(l):
return l+2

ret1=map(fun2,list1)
print(ret1)
# <map object at 0x000000E5A604B9E8>
print(list(ret1))
# [3, 4, 5, 6]``````
``````#reduce函数
# def reduce(function, sequence, initial=None): # real signature unknown; restored from __doc__
#     """
#     reduce(function, sequence[, initial]) -> value
#
#     Apply a function of two arguments cumulatively to the items of a sequence,
#     from left to right, so as to reduce the sequence to a single value.
#     For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
#     ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
#     of the sequence in the calculation, and serves as a default when the
#     sequence is empty.
#     """
#     pass

from functools import reduce
return x+y
``````a=5
``````#阶乘:函数式编程的方法，命令式编程比较直观