动态范围界定 - 深连结VS浅连结 [英] Dynamic Scoping - Deep Binding vs Shallow Binding

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

问题描述

我一直试图让我的头周围浅绑定和深连结,维基百科没有做正确解释它的一个好工作。说我有以下code,就输出什么,如果语言使用动态范围界定与

A)深的结合

二)浅约束力?

  X:整数:= 1
Y:整数:= 2过程添加
  X:= X + Y第二个程序(P:程序)
  X:整数:= 2
  P()程序首先
  Y:整数:= 3
  第二个(加)----主要从这里开始---
第一()
write_integer(X)


解决方案

深绑定结合环境的过程作为参数传递的时间

浅约束力结合环境的过程实际上是所谓的时间

因此​​,对于深绑定动态作用域时加传递到第二
环境为x = 1,Y = 3和x是全局x所以其写入4到全局x,它是由write_integer所述一个拾起。

浅的结合只是遍历,直到它找到对应名称最接近的变量,因此答案是1。

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) deep binding

b) shallow binding?

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

So for dynamic scoping with deep binding when add is passed into 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.

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

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

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