动态范围 - 深绑定与浅绑定 [英] Dynamic Scoping - Deep Binding vs Shallow Binding

查看:26
本文介绍了动态范围 - 深绑定与浅绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图弄清楚浅绑定和深绑定,维基百科没有很好地解释它.假设我有以下代码,如果语言使用动态范围

I've been trying to get my head around shallow binding and deep binding, wikipedia doesn't do a good job of explaining it properly. Say I have the following code, what would the output be if the language uses dynamic scoping with

a) 深度绑定

b) 浅绑定?

x: integer := 1
y: integer := 2

procedure add
  x := x + y

procedure second(P:procedure)
  x:integer := 2
  P()

procedure first
  y:integer := 3
  second(add)

----main starts here---
first()
write_integer(x)

推荐答案

深度绑定在过程作为参数传递时绑定环境

Deep binding binds the environment at the time the procedure is passed as an argument

浅绑定在实际调用过程时绑定环境

Shallow binding binds the environment at the time the procedure is actually called

因此,当 add 被传递到第二个时,对于具有深度绑定的动态范围环境是 x = 1, y = 3 并且 x 是全局 x 所以它将 4 写入全局 x,这是 write_integer 拾取的那个.

So for dynamic scoping with deep binding when add is passed into a second the environment is x = 1, y = 3 and the x is the global x so it writes 4 into the global x, which is the one picked up by the write_integer.

浅绑定只是向上遍历,直到找到与名称对应的最近变量,因此答案为 1.

Shallow binding just traverses up until it finds the nearest variable that corresponds to the name so the answer would be 1.

这篇关于动态范围 - 深绑定与浅绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆