cs61a 18 Spring——hw02 & lab02

函数式编程

  • 简化代码。
  • 定义函数时应该认真考虑inputs变量和body,将相同结构/过程/操作抽象化。
  • lambda 函数的运用: one-argument 式函数。
    • 要分清返回的是函数还是值(skill:Draw frame),只有()才是call expression
    • eg: lambda【函数】 x【自变量】:x【返回值】
    • 当函数F的参数是函数func时,一般return lambda x来输入func 的参数x。

High-Order Function

  • print(‘’)只输出符号中的值;return ‘’ 引号也输出
  • 同 lambda,要分清是将函数赋值,还是调用Call expression。 Skills:Draw frame
  • 核心是分析数学过程,合并重复结构/过程。

这次的作业很奇妙,把自己写lab02_extra中的答案也po上来

def compose1(f, g):
    """Return the composition function which given x, computes f(g(x)).

    >>> add_one = lambda x: x + 1        # adds one to x
    >>> square = lambda x: x**2
    >>> a1 = compose1(square, add_one)   # (x + 1)^2
    >>> a1(4)
    25
    >>> mul_three = lambda x: x * 3      # multiplies 3 to x
    >>> a2 = compose1(mul_three, a1)    # ((x + 1)^2) * 3
    >>> a2(4)
    75
    >>> a2(5)
    108
    """
    return lambda x: f(g(x))
def composite_identity(f, g):
    """
    Return a function with one parameter x that returns True if f(g(x)) is
    equal to g(f(x)). You can assume the result of g(x) is a valid input for f
    and vice versa.

    >>> add_one = lambda x: x + 1        # adds one to x
    >>> square = lambda x: x**2
    >>> b1 = composite_identity(square, add_one)
    >>> b1(0)                            # (0 + 1)^2 == 0^2 + 1
    True
    >>> b1(4)                            # (4 + 1)^2 != 4^2 + 1
    False
    """
    return lambda x: compose1(f, g)(x) == compose1(g, f)(x)
def count_cond(condition):
    """Returns a function with one parameter N that counts all the numbers from
    1 to N that satisfy the two-argument predicate function CONDITION.

    >>> count_factors = count_cond(lambda n, i: n % i == 0)
    >>> count_factors(2)   # 1, 2
    2
    >>> count_factors(4)   # 1, 2, 4
    3
    >>> count_factors(12)  # 1, 2, 3, 4, 6, 12
    6

    >>> is_prime = lambda n, i: count_factors(i) == 2
    >>> count_primes = count_cond(is_prime)
    >>> count_primes(2)    # 2
    1
    >>> count_primes(3)    # 2, 3
    2
    >>> count_primes(4)    # 2, 3
    2
    >>> count_primes(5)    # 2, 3, 5
    3
    >>> count_primes(20)   # 2, 3, 5, 7, 11, 13, 17, 19
    8
    """

    def count(n):
        i = 1
        cnt = 0
        while i <= n:
            if condition(n, i):
                cnt += 1
            i += 1
        return cnt
    return lambda n: count(n)
def cycle(f1, f2, f3):
    """Returns a function that is itself a higher-order function.

    >>> def add1(x):
    ...     return x + 1
    >>> def times2(x):
    ...     return x * 2
    >>> def add3(x):
    ...     return x + 3
    >>> my_cycle = cycle(add1, times2, add3)
    >>> identity = my_cycle(0)
    >>> identity(5)
    5
    >>> add_one_then_double = my_cycle(2)
    >>> add_one_then_double(1)
    4
    >>> do_all_functions = my_cycle(3)
    >>> do_all_functions(2)
    9
    >>> do_more_than_a_cycle = my_cycle(4)
    >>> do_more_than_a_cycle(2)
    10
    >>> do_two_cycles = my_cycle(6)
    >>> do_two_cycles(1)
    19
    """
    def result(n, x):
        if n == 0:
            return x
        elif n % 3 == 1:
            return f1(result(n - 1, x))
        elif n % 3 == 2:
            return f2(result(n - 1, x))
        elif n % 3 == 0:
            return f3(result(n - 1, x))
    return lambda n: lambda x: result(n, x)
上一篇:Photoshop 模特光滑的黄铜质感肤色


下一篇:递归函数怎么写