当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)05-Higher-Order Functions

资源类别:文库,文档格式:PPTX,文档页数:17,文件大小:716.21KB,团购合买
点击下载完整版文档(PPTX)

Higher-order Functions 9/30/2019 Slides adapted from Berkeley CS61a

Higher-Order Functions 9 / 30 / 2019 Slides adapted from Berkeley CS61a

Higher-Order Functions Functions are first-class,meaning they can be manipulated as values A higher-order function is: 1.A function that takes a function as an argument and/or 1.A function that returns a function as a return value

Higher-Order Functions Functions are first-class, meaning they can be manipulated as values A higher-order function is: 1. A function that takes a function as an argument and/or 1. A function that returns a function as a return value

Designing Functions

Designing Functions

Describing Functions def square(x): """Return X *x""" A function's domain is the set of all inputs it might x is a number possibly take as arguments. A function's range is the set of output values it square returns a non- might possibly return. negative real number A pure function's behavior is the relationship it square returns the square of x creates between input and output

Describing Functions A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output. def square(x): """Return X * X""" x is a number square returns a non￾negative real number square returns the square of x

A Guide to Designing Function Give each function exactly one job,but make it apply to many related situations >>>round(1.23) >>round(1.23,1) >>round(1.23,0) >>round(1.23,5) 1 1.2 1 1.23 Don't repeat yourself (DRY).Implement a process just once,but execute it many times

A Guide to Designing Function Give each function exactly one job, but make it apply to many related situations >>> round(1.23) 1 >>> round(1.23, 1) 1.2 >>> round(1.23, 0) 1 >>> round(1.23, 5) 1.23 Don’t repeat yourself (DRY). Implement a process just once, but execute it many times

Generalization

Generalization

Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: 0 Area: 0r2 元2 3v3) 2 Finding common structure allows for shared implementation Demo

Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: Area: Finding common structure allows for shared implementation Demo

Higher-Order Functions

Higher-Order Functions

Generalizing Over Computational Processes The common structure among functions may be a computational process,rather than a number. 5 =1+2+3+4+5 =15 k=1 5 =13+23+3+43+53 =225 k=1 51 =- 8 8 8 8 8 3+ =3.04 35 99 195 + 323 Demo

Generalizing Over Computational Processes The common structure among functions may be a computational process, rather than a number. Demo

Summation Example Function of a single argument def cube(k): (not called "term") return pow(k,3) A formal parameter that will def summation(n,term) be bound to a function """Sum the first n terms of a sequence. >>summation(5,fcube) 225) n鞋 The cube function is passed 0+1+8+27+64+125 total,k =0,1 as an argument value while k <n: total,k total +'term(k),k 1 return total The function bound to term gets called here

Summation Example def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total Function of a single argument (not called "term") A formal parameter that will be bound to a function The cube function is passed 0 + 1 + 8 + 27 + 64 + 125 as an argument value The function bound to term gets called here

点击下载完整版文档(PPTX)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共17页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有