表达式 ((lambda (x) (* x 2)) (+ 3 1)) 的求值顺序?

Evaluation order for the expression ((lambda (x) (* x 2)) (+ 3 1))?

一个

((lambda (x) (* x 2)) (+ 3 1))
((lambda (x) (* x 2)) 4)
(* 4 2)
8

B

((lambda (x) (* x 2)) (+ 3 1))
(* (+ 3 1) 2)
(* 4 2)
8

我猜有两个版本。但是我不确定哪个是正确的。

第二个使用 SICP 所谓的替换。这不是真正的 lisp 所做的,它在第 1 章中被描述为学习练习。一旦引入像 let 这样的特殊形式,这就不起作用了。

第一个使用applicative order,这是像racket这样的真正的scheme实现。它计算 lambda,然后计算以下表达式,然后将 lambda 生成的函数应用于计算 (+ 3 1) 的结果。

This is how Gerald Sussman explains his use of the substitution model:

If we're going to understand processes and how we control them, then we have to have a mapping from the mechanisms of this procedure into the way in which these processes behave. What we're going to have is a formal, or semi-formal, mechanical model whereby you understand how a machine could, in fact, in principle, do this. Whether or not the actual machine really does what I'm about to tell you is completely irrelevant at this moment.

In fact, this is an engineering model, in the same way that, [for an] electrical resistor, we write down a model V = IR — it's approximately true, but it's not really true; if I put enough current through the resistor, it goes boom, so the voltage is not always proportional to the current, but for some purposes the model is appropriate.

In particular, the model we're going to describe right now, which I call the substitution model, is the simplest model that we have for understanding how procedures work and how processes work — how procedures yield processes.

And that substitution model will be accurate for most of the things we'll be dealing with in the next few days. But eventually, it will become impossible to sustain the illusion that that's the way the machine works, and we'll go to other, more specific and particular models that will show more detail.