SICP-Notes-Lecture 04 Higher-Order Function

Lecture 04 Higher-Order Functions

These are my notes for SICP(Structure and Interpretation of Computer Programs). Hope they’ll be of some help to you.

Descriptions

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

Generalization

generalizing patterns with arguments
from math import pi, sqrt

def area_square(r):
    assert r > 0, 'The input length must be positive' 
    #assert <boolean expression>, '<words printed when the value of the boolean expression is false>'
    return r * r

def area_circle(r):
    assert r > 0, 'The input length must be positive'
    return pi * r * r

def area_hexagon(r):
    assert r > 0, 'The input length must be positive'
    return 3 * sqrt(3) * r * r / 2
  • Finding common structure allows for shared implementation
from math imort pi, sqrt

def area(r, shape_constant): #using arguement to generalize similar patterns
    assert r > 0, 'The input length must be positive'
    return r * r * shape_constant #a kind of superior abstraction

def area_square(r):
    return area(r, 1)

def area_circle(r):
    return area(r, pi)

def area_hexagon(r):
    return area(r, 3 * sqrt(3) / 2)
generalizing over-computational processes
def sum_naturals(n):
    i, total = 1, 0
    while i <= n:
        total, i = total + i, i + 1
    return total

def sum_cubes(n):
    i, total = 1, 0
    while i <= n:
        total, i = total + pow(i, 3), i + 1
    return total
  • The common structure among functions may be a computational process, rather than a number.
def summation(n, term): #here term is a parameter that will be bound to a function
	""" Sum the first n terms of a sequence
	
	>>> summation(5, cube)
	225
	>>> summation(5, identity) #The cube function is passed as an argument value
	15
	"""
    total, k = 0, 1
    while k <= n:
        total, k = total + term(k), k + 1 #The function bound to term gets called here
    return total

def identity(k):
    return k

def cube(k): #function of a single argument
    return pow(k, 3)

def sum_naturals(n):
    return summation(n, identity)

def sum_cubes(n):
    return summation(n, cube)
functions as return values

Functions defined within other function bodies are bound to names in a local frame

def make_adder(n): #A function that returns a function
    """Return a function that takes one argument k and returns k + n.
    
    >>> add_three = make_adder(3) #The name add_three is bound to a function
    >>> add_three(4)
    7
    """
    def adder(k): #A def statement within annother def statement
        return k + n #Can refer to names in the enclosing function
    return adder
  • the concept of scope
call expressions as operator
>>> make_adder(1)(2)
3

make_adder(1) : An call expression that evaluates to a function

make_adder(1)(2): An nested call expression that eventually evalutes to the outside function’s return value

Summary

  • Higher-order-function: any function that either accepts a function as an argument and/or returns a function
  • Why are these useful?
    • Generalize over different form of computation
    • Helps remove repetitive segments of code
  • We saw nested functions(closures) can access variables in outer function through static scoping.

A more complex example

def make_adder(n):
    def adder(k):
        return k + n
    return adder

def square(x):
    return x * x


def compose1(f, g):
    def h(x):
        return f(g(x))
    return h
>>> compose1(square, make_adder(2))(3)
25
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值