倫佐假設論證已經在列表中,一些解釋者可能是正確的。 eval
的大部分驅動都是這樣的,然後實際的lambda實現在apply
之前執行evlis
。
效率最高的Scheme實現將代碼作爲堆棧機器執行,因此每個變量只是一個偏移指向堆棧的指針,就像在本機程序中一樣。對於(lambda l l)
工作l
需要從所有參數conse,以便在該函數的開始執行O((length n))
任務,並且它有一個堆棧參數與該新創建列表的地址。然後它返回該地址,也許將它留在堆棧上。
append
獲取列表作爲兩個參數。因此它不需要從堆棧創建它們,因爲堆棧有兩個地址。 append
製作l1
的副本,當l1
是空列表時,它使用l2
而不用任何任何東西作爲最後一對的cdr`。舉個例子:
(define test1 '(1 2 3))
(define test2 '(4 5 6))
(define test3 (append test1 test2))
test3 ; ==> (1 2 3 4 5 6)
(eq? (cdddr test3) test2) ; ==> #t (they are the same)
(define test4 (append test1 '()))
test4 ; ==> (1 2 3)
(equal? test1 test4) ; ==> #t
(eq? test1 test4) ; ==> #f (they just look the same)
這裏是參與第一append
步驟:
(append '(1 2 3) '(4 5 6)) ; ==
(cons '1 (append '(2 3) '(4 5 6)) ; ==
(cons '1 (cons '2 (append '(3) '(4 5 6))) ; ==
(cons '1 (cons '2 (cons 3 (append '() '(4 5 6)))) ; ==
(cons '1 (cons '2 (cons 3 '(4 5 6))) ; ==
正如你可以看到它是O((+ 1 (length l1)))
喔,所以我的一套解釋!是不正確的,感謝 – Naman
但effiecent方案實現比如球拍的Ikarus保持它們的參數在堆棧上。堆棧分配的參數如何變成O(1)對的鏈? – Sylwester