博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SICP-1.6-高阶函数
阅读量:6876 次
发布时间:2019-06-26

本文共 3064 字,大约阅读时间需要 10 分钟。

高阶函数

  • 将函数作为参数
  • 例如
    • 1 def sum_naturals(n):2         total, k = 0, 13         while k <= n:4             total, k = total + k, k + 15         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 totaldef cube(x):    return x*x*xdef sum_cubes(x):    return summation(x,cube)
    • 黄金比例
    • def improve(update, close, guess=1):        while not close(guess):            guess = update(guess)        return guessdef golden_update(guess):        return 1/guess + 1def square_close_to_successor(guess):        return approx_eq(guess * guess, guess + 1)def approx_eq(x, y, tolerance=1e-15):        return abs(x - y) < toleranceimprove(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 * xdef successor(x):    return x + 1def compose1(f,g):    def h(x):        return f(g(x))    return hsquare_successor = compose1(square,successor)result = square_successor(12)

     

牛顿法

  • def newton_update(f, df):        def update(x):            return x - f(x) / df(x)        return updatedef 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)

转载于:https://www.cnblogs.com/EliEyes/p/7055450.html

你可能感兴趣的文章
上传文件大小限制
查看>>
注意A链接的默认行为
查看>>
有序的组合
查看>>
MySQL----基本数据类型
查看>>
wpgcms---详情页面数据怎么渲染
查看>>
Postman中Collection Runner添加外部数据文件
查看>>
JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、jinfo、jconsole使用详解
查看>>
验证DG最大性能模式下使用ARCH/LGWR及STANDBY LOG的不同情况
查看>>
【IOI2000】【洛谷1435】回文字串
查看>>
php数组的各种排序
查看>>
值得再次看的题目
查看>>
MySQL基础
查看>>
js作用域
查看>>
PHP语言 -- 数据访问,连接数据库
查看>>
GCD的使用和面试题集锦
查看>>
PIC18FXX2之INT0中断
查看>>
Vue-input框checkbox强制刷新
查看>>
bootstrap(7)关于进度条,列表,面板
查看>>
缓冲区溢出基础实践(二)——ROP 与 hijack GOT
查看>>
xml文件,xml格式字符串 读写
查看>>