在LISP中,“let”和“with”之间有什么区别?

时间:2012-02-06 02:58:02

标签: lisp substitution let

一个显示行动差异的简单例子真的会有所帮助,因为对我而言,它们似乎都可以互换?谢谢:))

2 个答案:

答案 0 :(得分:1)

至少在Common Lisp中,您只能在with宏的上下文中使用loop。请参阅Common Lisp Hyperspec

答案 1 :(得分:1)

它们在功能上确实相同。

事实上,至少SBCL将with宏调用中loop的任何使用扩展为封闭的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))))))))