一个简单的示例可以展示它们之间的区别,因为在我看来它们似乎是可以互换的?谢谢 :)
它们确实在功能上等效。
事实上,至少SBCL会将loop
宏调用中任何使用with
的地方扩展为一个封闭的LET
表达式。
运行以下内容:
(macroexpand '(loop with foo = 5 repeat 10 collect foo))
结果如下展开:
(BLOCK NIL
(LET ((FOO 5))
(LET ((#:LOOP-REPEAT-1681 (CEILING 10)))
(DECLARE (TYPE INTEGER #:LOOP-REPEAT-1681))
(SB-LOOP::WITH-LOOP-LIST-COLLECTION-HEAD (#:LOOP-LIST-HEAD-1682
#:LOOP-LIST-TAIL-1683)
(SB-LOOP::LOOP-BODY NIL
((IF (<= #:LOOP-REPEAT-1681 0)
(GO SB-LOOP::END-LOOP)
(DECF #:LOOP-REPEAT-1681)))
((SB-LOOP::LOOP-COLLECT-RPLACD
(#:LOOP-LIST-HEAD-1682 #:LOOP-LIST-TAIL-1683)
(LIST FOO)))
((IF (<= #:LOOP-REPEAT-1681 0)
(GO SB-LOOP::END-LOOP)
(DECF #:LOOP-REPEAT-1681)))
((RETURN-FROM NIL
(SB-LOOP::LOOP-COLLECT-ANSWER
#:LOOP-LIST-HEAD-1682))))))))