有人告诉我,在 lisp 中,let
比 prog
快(但 prog
有更多的灵 active ),当制作变量范围。我的问题是:为什么?我的意思是,有些时候使用 prog
更容易,但是,除了经验测试,我不知道如何猜测效果。是分配内存的时候吗?是执行吗?循环的时候会不会来得更多?我不知道实现差异的细节。
最佳答案
口齿不清
prog
的描述说:
prog
can be explained in terms ofblock
,let
, andtagbody
as follows:
(prog variable-list declaration . body)
== (block nil (let variable-list declaration (tagbody . body)))
换句话说,从功能上讲,prog
是
let
(除了一个小插图:前者返回 nil
而后者返回其最后形式的 value(s))。
一句谚语"sufficiently smart
compiler" - 实际上,
任何现代 Lisp 编译器 - 可以检测到
return
和
go
不使用并编译
prog
与等效的 let
相同:
(disassemble '(lambda () (let ((a 1) (b 2)) (print (+ a b)) nil)))
和
(disassemble '(lambda () (prog ((a 1) (b 2)) (print (+ a b)))))
产生相同的输出:
Disassembly of function :LAMBDA
(CONST 0) = 1
(CONST 1) = 2
0 required arguments
0 optional arguments
No rest parameter
No keyword parameters
7 byte-code instructions:
0 (CONST&PUSH 0) ; 1
1 (CONST&PUSH 1) ; 2
2 (CALLSR&PUSH 2 55) ; +
5 (PUSH-UNBOUND 1)
7 (CALLS1 142) ; PRINT
9 (NIL)
10 (SKIP&RET 1)
NIL
Cadence SKILL++
您可能想询问实现者。
关于scope - prog 与 let in LISP 性能差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49902313/