在Scheme中进行函数调用时会创建多少个环境框架

时间:2016-11-29 22:27:25

标签: functional-programming scheme

如您所知,当有函数调用时,Scheme会为每个调用创建一个新位置,并将函数参数放入位置。 例如,当你有

(define r 5)
(define a 3)
(define (f x) (* x r))

following locations model是在后台创建的:

{E} (f a)
{E} (f 3)
{E1} (* x r)
Final: 15

我的问题是,当您执行此代码时,会创建多少个位置以及位置模型的外观:

(define (f a)
  (define (g b) (* a b))
  (g (g (+ a 1))))

(f 3) -> 36

谢谢!

1 个答案:

答案 0 :(得分:2)

如果我理解你的问题,创建的环境框架如下:

  • E0(顶级;上下文= int):f =#< procedure>
  • E1(parent = E0; context = (define (f a) ...)):a = 3
  • E2(parent = E1; context = (f 3)):g =#< procedure>
  • E3(parent = E2; context = (define (g b) ...)):lhs = 3,rhs = 1
  • E4(parent = E2; context = inner (+ a 1)):b = 4
  • E5(parent = E4; context = (g ...)):lhs = 3,rhs = 4
  • E6(parent = E2; context = outer (* a b)):b = 12
  • E7(parent = E6; context = (g ...)):lhs = 3,rhs = 12