动态-shallow绑定和绑定深 [英] Dynamically -shallow binding and deep binding

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

问题描述

我工作的这个问题,我得到了答案:

静态:13

动态 - 深绑定:2'; - 我不知道这一个。

动态-shallow绑定:2'; - 我不知道这一个。

是正确的?

考虑下面的程序(在一个Pascal语言一样)。什么是的输出
语言是静态范围?是什么语言的输出动态范围
并使用深层约束力?是什么语言的输出动态范围和
采用浅约束力?

 项目为主;
   X:整数:= 2;
   Y:整数:= 1;
步骤F3(Z:整数)
   开始
       X = Z + X + Y;
   结束
步骤F2(P:过程中,Z:整数)
     INT X:= 5;
   开始
              P(Z)
   结束
步骤F1(Z:整数)
      INT Y:= Z
   开始
     F2(F3,Y);
  结束开始/ *主程序* /
F1(4);
       打印(X)
结束


解决方案

对于静态范围和动态范围与 - 浅结合的情况下,为什么不试试呢?使用Perl静态范围:

 我的$ X = 2;
我的$ Y = 1;
子F3($){
  我的$ Z =转变;
  $ X = $ Z + $ X + $ Y;
}
子F2($$){
  我的($ P,$ Z)= @_;
  我的$ X = 5;
  $ P->($ Z);
}
子F1($){
  我的$ Z =转变;
  我的$ Y = $ Z者除外;
  F2(\\&放大器; F3,$ Y);
}
F1(4);
打印$ X \\ n;

我得到 7 (这是 4 + 2 + 1 )。更改我的 s到本地 s到获得与浅的结合,我得到动态范围2 ,因为你predicted。

测试了深绑定动态范围是棘手的,因为这么几种语言支持。在这个回答前阵子,我贴的Perl code深实现绑定手动通过传递一个哈希对标量的参考;使用同样的方法:

 #!的/ usr / bin中/ perl的-w使用警告;
使用严格的;#创建一个新的标,它初始化到规定值,
#并返回到它的参考:
子new_scalar($)
  {返回\\(移); }#绑定指定的程序到指定的环境:
子bind_proc(\\%$)
{
  我的$ V = {%{+ SHIFT}};
  我的$ F =转变;
  返回子{$ F->($ V,@_); };
}我的$ V = {};$ V-> {X} = new_scalar 2;
$ V-> {Y} = new_scalar 1;子F3(\\%$){
  我的$ V =转变;
  我的$ Z = $ V-> {Z}; #保存现有ž
  $ V-> {Z} = new_scalar转变; #创建和放大器;初始化新的z
  $ {$ V-> {X} = $ {$ V-> {Z}} + $ {$ V-> {X}} + $ {$ V-> {Y}};
  $ V-> {Z} = $ Z者除外; #恢复旧ž
}
子F2(\\%$$){
  我的$ V =转变;
  我的$ P =转变;
  我的$ Z = $ V-> {Z}; #保存现有ž
  $ V-> {Z} = new_scalar转变; #创建和放大器;初始化新的z
  我的$ X = $ V-> {X}; #保存现有的X
  $ V-> {X} = new_scalar 5; #创建和放大器;初始化新的x
  $ P->($ {$ V-> {Z}});
  $ V-> {X} = $ X; #恢复旧点¯x
  $ V-> {Z} = $ Z者除外; #恢复旧ž
}
子F1(\\%$){
  我的$ V =转变;
  我的$ Z = $ V-> {Z}; #保存现有ž
  $ V-> {Z} = new_scalar转变; #创建和放大器;初始化新的z
  我的$ Y = $ V-> {Y}; #保存现有ÿ
  $ V-> {Y} = new_scalar $ {$ V-> {Z}}; #创建和放大器;初始化新的Y
  F2(%$ V,bind_proc(%$ V,\\&放大器; F3),$ {$ V-> {Y}});
  $ V-> {Y} = $ Y; #恢复旧ÿ
  $ V-> {Z} = $ Z者除外; #恢复旧ž
}
F1(%$ V,4);
打印$ {$ V-> {X}} \\ n;__结束__

我得到 10 (这是 4 + 2 + 4 )。

I'm working on this problem and I got the answers :

Statically: 13

Dynamically -deep binding: 2 <- i'm not sure about this one

Dynamically -shallow binding: 2 <- i'm not sure about this one

is that correct?

Consider the program below (in a Pascal like language). What is the output of the language is statically scoped? What is the output of the language is dynamically scoped and uses deep binding? What is the output of the language is dynamically scoped and uses shallow binding?

Program main;
   x: integer := 2;
   y: integer := 1;
procedure f3(z: integer)
   begin 
       x = z + x + y;
   end
procedure f2(p: procedure, z: integer)
     int x := 5;
   begin
              p(z) 
   end
procedure f1(z: integer)
      int y := z
   begin
     f2(f3,y);
  end

begin /* main program */
f1(4);
       print(x)
end

解决方案

For the static scope and the dynamic-scope-with-shallow-binding cases, why not try them out? Using Perl with static scope:

my $x = 2;
my $y = 1;
sub f3($) {
  my $z = shift;
  $x = $z + $x + $y;
}
sub f2($$) {
  my ($p, $z) = @_;
  my $x = 5;
  $p->($z);
}
sub f1($) {
  my $z = shift;
  my $y = $z;
  f2(\&f3, $y);
}
f1(4);
print "$x\n";

I get 7 (which is 4 + 2 + 1). Changing the mys to locals to get dynamic scope with shallow binding, I get 2, as you predicted.

Testing out dynamic scope with deep binding is trickier, because so few languages support it. In this answer a while back, I posted Perl code that implemented deep binding "manually" by passing around a hash of references to scalars; using that same approach:

#!/usr/bin/perl -w

use warnings;
use strict;

# Create a new scalar, initialize it to the specified value,
# and return a reference to it:
sub new_scalar($)
  { return \(shift); }

# Bind the specified procedure to the specified environment:
sub bind_proc(\%$)
{
  my $V = { %{+shift} };
  my $f = shift;
  return sub { $f->($V, @_); };
}

my $V = {};

$V->{x} = new_scalar 2;
$V->{y} = new_scalar 1;

sub f3(\%$) {
  my $V = shift;
  my $z = $V->{z};                 # save existing z
  $V->{z} = new_scalar shift;      # create & initialize new z
  ${$V->{x}} = ${$V->{z}} + ${$V->{x}} + ${$V->{y}};
  $V->{z} = $z;                    # restore old z
}
sub f2(\%$$) {
  my $V = shift;
  my $p = shift;
  my $z = $V->{z};                 # save existing z
  $V->{z} = new_scalar shift;      # create & initialize new z
  my $x = $V->{x};                 # save existing x
  $V->{x} = new_scalar 5;          # create & initialize new x
  $p->(${$V->{z}});
  $V->{x} = $x;                    # restore old x
  $V->{z} = $z;                    # restore old z
}
sub f1(\%$) {
  my $V = shift;
  my $z = $V->{z};                 # save existing z
  $V->{z} = new_scalar shift;      # create & initialize new z
  my $y = $V->{y};                 # save existing y
  $V->{y} = new_scalar ${$V->{z}}; # create & initialize new y
  f2(%$V, bind_proc(%$V, \&f3), ${$V->{y}});
  $V->{y} = $y;                    # restore old y
  $V->{z} = $z;                    # restore old z
}
f1(%$V, 4);
print "${$V->{x}}\n";

__END__

I get 10 (which is 4 + 2 + 4).

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

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