栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

SICP--1.6---高阶函数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SICP--1.6---高阶函数

SICP--1.6---高阶函数
高阶函数

将函数作为参数
例如

1 def sum_naturals(n):
2  total, k = 0, 1
3  while k <= n:
4      total, k = total + k, k + 1
5  return total
def sum_cubes(n):
 total, k = 0, 1
 while k <= n:
     total, k = total + k*k*k, k + 1
 return total
def pi_sum(n):
 total, k = 0, 1
 while k <= n:
     total, k = total + 8 / ((4*k-3) * (4*k-1)), k + 1
 return total

以上三个例子中有许多共同的部分
def (n):
total, k = 0, 1
while k <= n:
total, k = total + (k), k + 1
return total
高阶函数形式

def summation(n, term):
    total, k = 0, 1
    while k <= n:
 total, k = total + term(k), k + 1
    return total

def cube(x):
    return x*x*x

def sum_cubes(x):
    return summation(x,cube)

函数环境
黄金比例

def improve(update, close, guess=1):
 while not close(guess):
     guess = update(guess)
 return guess

def golden_update(guess):
 return 1/guess + 1

def square_close_to_successor(guess):
 return approx_eq(guess * guess, guess + 1)

def approx_eq(x, y, tolerance=1e-15):
 return abs(x - y) < tolerance

improve(golden_update, square_close_to_successor)

高阶函数的不足
在全局环境中名称复杂
每一个函数的形参个数是由限制的
函数的嵌套定义

解决高阶函数存在的不足

def sqrt(a):
 def sqrt_update(x):
     return average(x, a/x)
 def sqrt_close(x):
     return approx_eq(x * x, a)
 return improve(sqrt_update, sqrt_close)

嵌套定义中的函数作用域
每一个嵌套的函数在定义函数内环境

不是函数的调用处

函数作用域的实现方法
每一个函数都有他的父环境
当函数被调用时,在其父环境中评估
函数作用域的好处
局部环境中的绑定不会影响全局环境

def square(x):
    return x * x

def successor(x):
    return x + 1

def compose1(f,g):
    def h(x):
 return f(g(x))
    return h

square_successor = compose1(square,successor)
result = square_successor(12)

牛顿法

def newton_update(f, df):
 def update(x):
     return x - f(x) / df(x)
 return update
def find_zero(f, df):
 def near_zero(x):
     return approx_eq(f(x), 0)
 return improve(newton_update(f, df), near_zero)
def square_root_newton(a):
 def f(x):
     return x * x - a
 def df(x):
     return 2 * x
 return find_zero(f, df)
Currying
>>> def curried_pow(x):
 def h(y):
     return pow(x, y)
 return h
>>> curried_pow(2)(3)
8
def curry2(f):
 """Return a curried version of the given two-argument function."""
 def g(x):
     def h(y):
  return f(x, y)
     return h
 return g
def uncurry2(g):
 """Return a two-argument version of the given curried function."""
 def f(x, y):
     return g(x)(y)
 return f

环境图
匿名函数

可以看做

       lambda     x     :   f(g(x))
"A function that    takes x    and returns     f(g(x))"

函数装饰器

>>> def trace(fn):
 def wrapped(x):
     print('-> ', fn, '(', x, ')')
     return fn(x)
 return wrapped

@trace
def triple(x):
return 3 * x
triple(12)
-> ( 12 )
36
@trace等同于

>>> def triple(x):
 return 3 * x
>>> triple = trace(triple)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/225833.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号