CS 61A 2020 Fall Disc 02: Higher-Order Functions, Self Reference

本文探讨了如何使用高阶函数实现条件判断打印、延迟打印等功能。通过实例解析环境图的绘制,并讲解如何创建返回新函数的函数。同时,介绍了自我引用的概念,以及如何构造一个可以重复打印n次后结束的函数。
摘要由CSDN通过智能技术生成

1.1 Write a function that takes in a function cond and a number n and prints numbers from 1 to n where calling cond on that number returns True. (需讨论)

def keep_ints(cond, n):
"""Print out all integers 1..i..n where cond(i) is true
>>> def is_even(x):
... # Even numbers have remainder 0 when divided by 2.
... return x % 2 == 0
>>> keep_ints(is_even, 5)
2
4
"""
    i = 1
    while i < n:
        if cond(i):
            print(i)
    i += 1


1.2 Tutorial: Write a function similar to keep_ints like before, but now it takes in a number n and returns a function that has one parameter cond. The returned function prints out numbers from 1 to n where calling cond on that number returns True.

def make_keeper(n):
"""Returns a function which takes one parameter cond and prints out
all integers 1..i..n where calling cond(i) returns True.
>>> def is_even(x):
... # Even numbers have remainder 0 when divided by 2.
... return x % 2 == 0
>>> make_keeper(5)(is_even)
2
4
"""

和上面的一样?


1.3 Draw the environment diagram that results from executing the code below. 

1 def curry2(h):
2 def f(x):
3 def g(y):
4 return h(x, y)
5 return g
6 return f
7 make_adder = curry2(lambda x, y: x + y)
8 add_three = make_adder(3)
9 add_four = make_adder(4)
10 five = add_three(2)


1.4 Write curry2 as a lambda function. 

curry2 = lambda h: lambda x: lambda y: h(x, y)

1.5 Tutorial: Draw the environment diagram that results from executing the code below.  

n = 7
def f(x):
    n = 8
    return x + 1

def g(x):
    n = 9
    def h():
        return x + 1
    return h

def f(f, x):
    return f(x + n)

f = f(g, n)
g = (lambda y: y())(f)


1.6 The following question is more challenging than the previous ones. Nonethe-
less, it’s a fun problem to try.

Draw the environment diagram that results from executing the code below.
Note that using the + operator with two strings results in the second string
being appended to the first. For example "C" + "S" concatenates the two
strings into one string "CS"

y = "y"
h = y
def y(y):
    h = "h"
    if y == h:
        return y + "i"
    y = lambda y: y(h)
    return lambda h: y(h)
y = y(y)(y)

 


1.7 Write a function print delayed that delays printing its argument until the
next function call. print delayed takes in an argument x and returns a
new function delay print. When delay print is called, it prints out x and
returns another delay print. 

def print_delayed(x):
"""Return a new function. This new function, when called,
will print out x and return another function with the same
behavior.
>>> f = print_delayed(1)
>>> f = f(2)
1
>>> f = f(3)
2
>>> f = f(4)(5)
3
4
>>> f("hi")
5
<function print_delayed> # a function is returned
"""
def print_delayed(x):
    def delay_print(y):
        print(x)
        return print_delayed(y)
    return delay_print

1.8 Tutorial: Write a function print n that can take in an integer n and returns
a repeatable print function that can print the next n parameters. After the
nth parameter, it just prints ”done”.

def print_n(n):
"""
>>> f = print_n(2)
>>> f = f("hi")
hi
>>> f = f("hello")
hello
>>> f = f("bye")
done
>>> g = print_n(1)
>>> g("first")("second")("third")
first
done
done
<function inner_print>
"""
def print_n(n):
    def inner_print(x):
        if n <= 0:
            print("done")
        else:
            print(x)
        return print_n(n-1)
    return inner_print

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值