CS 61A: SICP - week2 - 3

·  阅读 97

Environments

  1. Environments for Higher-Order Functions

1.1 Environments Enable Higher-Order Functions

Functions are first-class: Functions are values in our programming language
Higher-order function: A function that takes a function as an argument value or A function that returns a function as a return value

1.2 Names can be Bound to Functional Arguments

Applying a user-defined function:
• Create a new frame
• Bind formal parameters (f & x) to arguments
• Execute the body
  1. Environments for Nested Definitions

    2.1 Environment Diagrams for Nested Def Statements

    • Every user-defined function has a parent frame (often global)
    • The parent of a function is the frame in which it was defined
    • Every local frame has a parent frame (often global)
    • The parent of a frame is the parent of the function called
    

    2.2 How to Draw an Environment Diagram

    a. When a function is defined:

      Create a function value: func <name>(<formal parameters>) [parent=<label>] 
      Its parent is the current frame. 
      Bind <name> to the function value in the current frame
    

    b. When a function is called:

      1. Add a local frame, titled with the <name> of the function being called.
      2. Copy the parent of the function to the local frame: [parent=<label>]
      3. Bind the <formal parameters> to the arguments in the local frame.
      4. Execute the body of the function in the environment that starts with the local frame.
    
  2. Local Names

Local Names are not Visible to Other (Non-Nested) Functions

• An environment is a sequence of frames.
• The environment created by calling a top-level function (no def within def) consists of one local frame, followed by the global frame.
  1. Self-Reference

image.png

这个,你算对了吗

分类:
代码人生
标签:
分类:
代码人生
标签:
收藏成功!
已添加到「」, 点击更改