[2021 spring] CS61A Lab 2: Higher-Order Functions, Lambda Expressions

[2021 spring] CS61A Lab 2: Higher-Order Functions, Lambda Expressions,第1张

概述lab02:https://inst.eecs.berkeley.edu/~cs61a/sp21/lab/lab02/#topics目录快速回顾:LambdaExpressionsEnvironmentDiagramsAssignmentStatementsdefStatementsCallexpressionsLambdas必答题requiredquestionswhatwouldpythondisplayQ1:WWPD:LambdatheFreeQ2:WWPD:

lab02:https://inst.eecs.berkeley.edu/~cs61a/sp21/lab/lab02/#topics

目录快速回顾:Lambda ExpressionsEnvironment DiagramsAssignment Statementsdef StatementsCall expressionsLambdas必答题 required questionswhat would python displayQ1: WWPD: Lambda the FreeQ2: WWPD: Higher Order FunctionsCoding PracticeQ3: Lambdas and CurryingQ4: Count van CountEnvironment Diagram PracticeQ5: Make AdderQ6: Lambda the Environment DiagramSubmitOptional QuestionsQ7: Composite Identity FunctionQ8: I Heard You Liked Functions...ok autograde结果

快速回顾:Lambda Expressions


Environment Diagrams

Assignment Statements

def Statements

Call Expressions

Lambdas

必答题 required questionswhat would python displayQ1: WWPD: Lambda the Free

python ok -q lambda -u --local

部分问题:


Q2: WWPD: Higher Order Functions

python ok -q hof-wwpd -u --local


Coding PracticeQ3: Lambdas and Currying


先考虑def定义,再用lambda重写成一行。

def lambda_curry2(func):    def f(x):        def g(y):            return func(x, y)        return g    return f
def lambda_curry2(func):    return lambda x: lambda y: func(x, y)
Q4: Count van Count



观察重复代码,重构:

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, where    the first argument for Condition is N and the second argument is the    number from 1 to N.    def g(n):        i = 1        count = 0        while i <= n:            if condition(n, i):                count += 1            i += 1        return count    return g
Environment Diagram Practice

There is no submission for this component. However, we still encourage you to do these problems on paper to develop familiarity with Environment Diagrams, which might appear in an alternate form on the exam.

Q5: Make Adder

Q6: Lambda the Environment Diagram

submit

python ok --submit

Optional QuestionsQ7: Composite IDentity Function


比较f(g(x))和g(f(x))即可:python ok -q composite_IDentity --local

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.    return lambda x: compose1(f, g)(x) == compose1(g, f)(x)
Q8: I Heard You liked Functions...



先定义特例,再取模循环使用compose1:

def cycle(f1, f2, f3):    def g(n):        if n == 0:            return lambda x: x        if n == 1:            return f1        tmp = f1        i = 2        while i <= n:            if i % 3 == 1:                tmp = compose1(f1, tmp)            elif i % 3 == 2:                tmp = compose1(f2, tmp)            else:                tmp = compose1(f3, tmp)            i += 1        return tmp    return g
ok autograde结果

全部通过:

总结

以上是内存溢出为你收集整理的[2021 spring] CS61A Lab 2: Higher-Order Functions, Lambda Expressions全部内容,希望文章能够帮你解决[2021 spring] CS61A Lab 2: Higher-Order Functions, Lambda Expressions所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1184879.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存