我是靠谱客的博主 执着店员,最近开发中收集的这篇文章主要介绍SICP-Notes-Lecture 05 Environment Diagrams,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Lecture 05 Environment Diagrams

These are my notes for SICP(Structure and Interpretation of Computer Programs). Hope they’ll be of some help to you.

Environment Diagrams

What ?

  • A visual tool to keep track of bindings and state of a computer program
  • It can be applied to similar languages aside of python

Why?

  • conceptual

    • understand why programs work the way they do
    • confidently predict how a program will behave
  • helpful for debugging

    • when you’re really stuck,

      diagramming code > staring at lines of cod

What have we seen so far

  • Assignment Statement
  • Def Statement
  • Call Expressions

Terminology: Frames

  • A frame keeps track of variable-to-value bindings
    • Every call expression has a corresponding frame
  • Global, a.k.a. the global frame, is the starting frame
    • It doesn’t correspond to a specific call expression
  • Parent frames
    • The parent of a function is the frame in which it was defined
    • If you can’t find a variable in the current frame, you check it’s parent, and so on. If you can’t find the variable, NameError

Review: Evaluation Order

Remember to evaluate the operator, then the operand(s), then apply the operator onto the operand(s)

The environment diagram should reflect Python’s evaluation.

Variable Lookup

  • Lookup name in the current frame
  • Lookup name in the parent frame, its parent frame, etc…
  • Stop at the global frame
  • If not found, an error is thrown

Important: There was no lookup done in f1 since the parent of f2 was Global

Evaluation vs Apply

A new frame is created only when an application happens. Evaluation will not create a new frame.

Lambda Expressions

Expressions that evaluate to functions!

>>> square = lambda x: x * x # A function with parameter x that returns the value of x * x
>>> square
<function <lambda> ... >
>>> square(4)
16
>>> x = square(5)
>>> x
25

Lambda Expressions vs def Statements

square = lambda x: x * x
def square(x):
return x * x
  • Both create a function with the same behavior
  • The parent frame of each function is the frame in which they were defined
  • Both bind the function to the same name
  • Only the def statement gives the function an intrinsic name
    • you need to use an assignment statement to bind the function created by lambda expression to a name in order to refer to it later

Summary

  • Environment Diagrams formalize the evaluation procedure for Python

    • Understanding them will help you think deeply about how the code that you are writing actually works
  • Lambda functions are similar to functions defined with def, but are nameless

  • A Higher Order Function is a function that either takes in functions as an argument (shown earlier) and/or returns a function as a return value (will see soon)

最后

以上就是执着店员为你收集整理的SICP-Notes-Lecture 05 Environment Diagrams的全部内容,希望文章能够帮你解决SICP-Notes-Lecture 05 Environment Diagrams所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部