[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-seo-665543-105":3,"detail-sidebar-cat-0-en-105":80,"doc-detail-665543-en":130},{"code":4,"msg":5,"data":6},0,"ok",{"site_id":7,"language":8,"slug":9,"title":10,"keywords":11,"description":12,"schema_data":13,"social_meta":73,"head_meta":75,"extra_data":77,"updated_unix":79},105,"en","cs-61a-environment-diagrams-higher-order-functions-spring-2023-discussion-2-february-1-2023","CS 61A Environment Diagrams, Higher-Order Functions - Spring 2023 Discussion 2 - February 1, 2023","","CS 61A Spring 2023 Discussion 2 focuses on environment diagrams for call expressions, lambda expressions, and higher-order functions. It explains how call evaluation creates new frames, including intrinsic function names, parent frames, parameter binding, and return values (including implicit None). It also covers why built-in or imported functions may be treated without new frames, and how lambdas are represented without intrinsic names. The worksheet includes guided diagram-drawing questions, including nested calls and predicting Python output.",{"@graph":14,"@context":72},[15,34,55],{"@type":16,"itemListElement":17},"BreadcrumbList",[18,23,27,31],{"item":19,"name":20,"@type":21,"position":22},"https://docshare.wps.com","Home","ListItem",1,{"item":24,"name":25,"@type":21,"position":26},"https://docshare.wps.com/document/","Document",2,{"item":28,"name":29,"@type":21,"position":30},"https://docshare.wps.com/document/exam/","Exam",3,{"item":32,"name":10,"@type":21,"position":33},"https://docshare.wps.com/document/cs-61a-environment-diagrams-higher-order-functions-spring-2023-discussion-2-february-1-2023/665543/",4,{"url":32,"name":10,"@type":35,"image":36,"author":41,"headline":10,"publisher":44,"fileFormat":47,"inLanguage":8,"description":12,"dateModified":48,"datePublished":49,"encodingFormat":47,"isAccessibleForFree":50,"interactionStatistic":51},"DigitalDocument",{"url":37,"@type":38,"width":39,"height":40},"https://docshare.wps.com/thumbnails/cs-61a-environment-diagrams-higher-order-functions-spring-2023-discussion-2-february-1-2023/665543.png","ImageObject",300,407,{"name":42,"@type":43},"SANS","Person",{"url":19,"name":45,"@type":46},"DocShare","Organization","application/pdf","2026-10-30","2026-10-29",true,{"@type":52,"interactionType":53,"userInteractionCount":22},"InteractionCounter",{"@type":54},"ViewAction",{"@type":56,"mainEntity":57},"FAQPage",[58,64,68],{"name":59,"@type":60,"acceptedAnswer":61},"How are call expressions evaluated in environment diagrams?","Question",{"text":62,"@type":63},"Evaluate the operator to a function, evaluate operands left to right, create a new frame with a unique index, intrinsic function name, and parent frame, bind formal parameters to argument values, then evaluate the function body until a return value is produced (or None if no explicit return).","Answer",{"name":65,"@type":60,"acceptedAnswer":66},"Why don’t environment diagrams draw new frames for built-in or imported functions?",{"text":67,"@type":63},"Because their internal implementations are unknown, so the diagram cannot accurately fill in the structure of a new frame for them.",{"name":69,"@type":60,"acceptedAnswer":70},"How are lambda expressions represented in environment diagrams and when is their return expression evaluated?",{"text":71,"@type":63},"A lambda evaluates to a function (a lambda function) but is not bound to a name by itself; the body/return expression is evaluated only when the lambda is called, similar to delayed execution in def-defined functions. In diagrams, lambdas use the lambda symbol because they lack intrinsic names.","https://schema.org",{"og:url":32,"og:type":74,"og:title":10,"og:site_name":45,"og:description":12},"article",{"robots":76,"canonical":32},"index,follow",{"doc_id":78,"site_id":7},665543,1793304832,{"code":4,"msg":81,"data":82},"success",[83,87,91,94,99,104,109,114,119,122,126],{"id":22,"doc_module":4,"doc_module_name":25,"category_name":84,"show_sort_weight":85,"slug":86},"Story & Novel",90,"story-novel",{"id":26,"doc_module":4,"doc_module_name":25,"category_name":88,"show_sort_weight":89,"slug":90},"Literature",80,"literature",{"id":33,"doc_module":4,"doc_module_name":25,"category_name":29,"show_sort_weight":92,"slug":93},70,"exam",{"id":95,"doc_module":4,"doc_module_name":25,"category_name":96,"show_sort_weight":97,"slug":98},5,"Comic",60,"comic",{"id":100,"doc_module":4,"doc_module_name":25,"category_name":101,"show_sort_weight":102,"slug":103},6,"Technology",50,"technology",{"id":105,"doc_module":4,"doc_module_name":25,"category_name":106,"show_sort_weight":107,"slug":108},7,"Healthcare",40,"healthcare",{"id":110,"doc_module":4,"doc_module_name":25,"category_name":111,"show_sort_weight":112,"slug":113},8,"Research & Report",30,"research-report",{"id":115,"doc_module":4,"doc_module_name":25,"category_name":116,"show_sort_weight":117,"slug":118},9,"Religion & Spirituality",20,"religion-spirituality",{"id":117,"doc_module":4,"doc_module_name":25,"category_name":120,"show_sort_weight":117,"slug":121},"World Cup","world-cup",{"id":123,"doc_module":4,"doc_module_name":25,"category_name":124,"show_sort_weight":123,"slug":125},10,"Lifestyle","lifestyle",{"id":127,"doc_module":4,"doc_module_name":25,"category_name":128,"show_sort_weight":95,"slug":129},19,"General","general",{"code":4,"msg":81,"data":131},{"doc_id":78,"user_id":132,"nickname":42,"user_avatar":133,"doc_module":4,"category_id":33,"category_name":29,"doc_title":10,"doc_description":12,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":22,"is_deleted":4,"is_public":22,"is_downloadable":22,"audit_status":22,"page_count":139,"language":140,"language_code":8,"site_id":7,"html_lang":8,"table_of_contents":141,"faqs":142,"seo_title":143,"seo_description":12,"update_tm":79,"read_time":112},962090760505,"https://ap-avatar.wpscdn.com/davatar_155a257f0dc6eb9ab79c44ca47cae57d","CS 61A Environment Diagrams, Higher-Order Functions  \nSpring 2023 Discussion 2: February 1, 2023  \nNote on web discussions: In order to use the environment diagrams on the site, please log in using the account you use for Okpy.  \nCall Expressions  \nCall expressions, such as square(2) , apply functions to arguments. When executing call expressions, we create anew frame in our diagram to keep track of local variables:  \n1. Evaluate the operator, which should evaluate to a function.  \n2. Evaluate the operands from left to right.  \n3. Draw a new frame, labelling it with the following:  \n• A unique index (f1 , f2 , f3 ,…) .  \n• The intrinsic name of the function, which is the name of the function object itself. For example, if the function object is func square(x) [parent=Global], the intrinsic name is square.  \n• The parent frame ([parent=Global]) .  \n4. Bind the formal parameters to the argument values obtained in step 2 ( e.g. bind x to 3) .  \n5. Evaluate the body of the function in this new frame until a return value is obtained. Write down the return value in the frame.  \nIf a function does not have a return value, it implicitly returns None. In that case, the “Return value” box should contain None.  \nNote: Since we do not know how built-in functions like min ( . . .) or imported functions like add( . . .) are implemented, we do not draw a new frame when we call them, since we would not be able to fill it out accurately.  \nQ1: Call Diagram  \nLet’s put it all together! Draw an environment diagram for the following code. You may not have to use all of the blanks provided to you.  \nSee the web version of this resource for the environment diagram.  \nVideo diagram  \n2 Environment Diagrams, Higher-Order Functions  \nQ2: Nested Calls Diagrams  \nDraw the environment diagram that results from executing the code below. You may not need to use all of the frames and blanks provided to you.  \nSee the web version of this resource for the environment diagram.  \nVideo walkthrough  \nNote: This worksheet is a problem bank—most TAs will not cover all the problems in discussion section.  \nEnvironment Diagrams, Higher-Order Functions 3  \nLambda Expressions  \nA lambda expression evaluates to a function, called a lambda function. For example, lambda y: x + y is a lambda expression, and can be read as “a function that takes in one parameter y and returns x + y.”  \nA lambda expression by itself evaluates to a function but does not bind it to a name. Also note that the return expression of this function is not evaluated until the lambda is called. This is similar to how defining a new function using a def statement does not execute the function’s body until it is later called.  \n>>> what = lambda x : x + 5 >>> what  \n\u003Cfunction \u003Clambda> at 0xf3f490>  \nUnlike def statements, lambda expressions can be used as an operator or an operand to a call expression. This is because they are simply one-line expressions that evaluate to functions. In the example below, (lambda y: y + 5) is the operator and 4 is the operand.  \n>>> (lambda y: y + 5)(4)  \n9  \n>>> (lambda f, x: f(x))(lambda y: y + 1, 10) 11  \nQ3: Lambda the Environment Diagram  \nDraw the environment diagram for the following code and predict what Python will output.  \nSee the web version of this resource for the environment diagram.  \nNote: This worksheet is a problem bank—most TAs will not cover all the problems in discussion section.  \n4 Environment Diagrams, Higher-Order Functions Higher Order Functions  \nA higher order function (HOF) is a function that manipulates other functions by taking in functions as arguments, returning a function, or both. For example, the function compose below takes in two functions as arguments and returns a function that is the composition of the two arguments.  \ndef composer(func1, func2):  \n\"\"\"Return a function f , such that f (x) = func1 (func2 (x)) . \"\"\"  \ndef f(x):  \nreturn func1(func2(x))  \nreturn f  \nHOFs are powerful abstraction tools that allow us to express certai","cbCaiof0BJXLer0V","https://ap.wps.com/l/cbCaiof0BJXLer0V","pdf",61576,12,"English","# Call Expressions\n## Q1: Call Diagram\n# Nested Calls Diagrams\n## Q2: Nested Calls Diagrams\n# Lambda Expressions\n## Q3: Lambda the Environment Diagram\n# Higher Order Functions\n## HOFs in Environment Diagrams\n# Q4: Make Adder","[{\"question\":\"How are call expressions evaluated in environment diagrams?\",\"answer\":\"Evaluate the operator to a function, evaluate operands left to right, create a new frame with a unique index, intrinsic function name, and parent frame, bind formal parameters to argument values, then evaluate the function body until a return value is produced (or None if no explicit return).\"},{\"question\":\"Why don’t environment diagrams draw new frames for built-in or imported functions?\",\"answer\":\"Because their internal implementations are unknown, so the diagram cannot accurately fill in the structure of a new frame for them.\"},{\"question\":\"How are lambda expressions represented in environment diagrams and when is their return expression evaluated?\",\"answer\":\"A lambda evaluates to a function (a lambda function) but is not bound to a name by itself; the body/return expression is evaluated only when the lambda is called, similar to delayed execution in def-defined functions. In diagrams, lambdas use the lambda symbol because they lack intrinsic names.\"}]","CS 61A Environment Diagrams, Higher-Order Functions - Spring 2023 Discussion 2 - February 1, 2023 | PDF"]