gpt4 book ai didi

lambda - 小阴谋家 : stuck on multiinsertLR&co example

转载 作者:行者123 更新时间:2023-12-04 05:33:56 26 4
gpt4 key购买 nike

经过一些工作后,我能够理解 multirember&co 函数,但我无法真正理解以下 multiinsertLR&co 代码(第 143 页):

(define multiinsertLR&co
(lambda (new oldL oldR lat col)
(cond
((null? lat)
(col '() 0 0))
((eq? (car lat) oldL)
(multiinsertLR&co
new
oldL
oldR
(cdr lat)
(lambda (newlat L R)
(col (cons new
(cons oldL newlat))
(add1 L) R))))
((eq? (car lat) oldR)
(multiinsertLR&co
new
oldL
oldR
(cdr lat)
(lambda (newlat L R)
(col (cons oldR
(cons new newlat))
L (add1 R)))))
(else
(multiinsertLR&co
new
oldL
oldR
(cdr lat)
(lambda (newlat L R)
(col (cons (car lat)
newlat) L R)))))))

这本书似乎没有解释评估函数时应该首先通过哪个 collector,所以我使用了 a-friend 收集器和 last- friend 收藏家分别在第 138 页和第 140 页进行了解释。使用任一收集器评估函数会导致以下错误(使用带有 petit chez 方案的跟踪函数):

>> (multiinsertLR&co 'salty 'fish 'chips '(chips and fish or fish and chips) last-friend)

|(multiinsertLR&co salty fish chips (chips and fish or fish and chips)
#<procedure>)
|(multiinsertLR&co salty fish chips (and fish or fish and chips)
#<procedure>)
|(multiinsertLR&co salty fish chips (fish or fish and chips) #<procedure>)
|(multiinsertLR&co salty fish chips (or fish and chips) #<procedure>)
|(multiinsertLR&co salty fish chips (fish and chips) #<procedure>)
|(multiinsertLR&co salty fish chips (and chips) #<procedure>)
|(multiinsertLR&co salty fish chips (chips) #<procedure>)
|(multiinsertLR&co salty fish chips () #<procedure>)
Exception: incorrect number of arguments to #<procedure>
Type (debug) to enter the debugger.

代码看了好几遍,都没有发现错误。如果有人有任何见解,请分享。如果有人还可以用一些很好的例子向我指出(相对而言)对延续的温和解释,那也将不胜感激。

最佳答案

这是我解决 Chris 的第二个示例的过程,因为我认为这可能对其他可能被卡住的人有所帮助。 (如有错误请指正!)

;; Anonymous functions (collectors) in mutliinsertLR&co here defined as named  
;; functions for reference purposes
(define left-col
(lambda (newlat L R)
(col (cons new
(cons oldL newlat))
(add1 L) R))) ; L counter gets incremented

(define right-col
(lambda (new lat L R)
(col (cons oldR
(cons new newlat))
L (add1 R)))) ; R counter gets incremented

(define else-col
(lambda (newlat L R)
(col (cons (car lat)
(newlat) L R)))) ; neither counter gets incremented

;; Let's step through an example to see how this works:
;;
;; 1. ENTRY TO FUNCTION
;; new = foo
;; oldL = bar
;; oldR = baz
;; lat = (goo bar baz qux quux)
;; col = list
;;
;; 1. Is lat null? No.
;; 2. Is car of lat equal to oldL? No.
;; 3. Is car of lat equal to oldR? No.
;; 4. Else? Yes. Recur on the function passing the new, oldL
;; oldR, (cdr lat) and the new collector.
;;
;; 2. FIRST RECURSIVE STEP
;; new = foo
;; oldL = bar
;; oldR = baz
;; lat = (bar baz qux quux)
;; col = else-col
;;
;; - Is lat null? No.
;; - Is car of lat equal to oldL? Yes. Recur on the function
;; passing new, oldL, oldR, (cdr lat), and the new col.
;;
;; 3. SECOND RECURSIVE STEP
;; new = foo
;; oldL = bar
;; oldR = baz
;; lat = (baz qux quux)
;; col = left-col
;;
;; - Is lat null? No.
;; - Is car of lat equal to oldL? No.
;; - Is car of lat equal to oldR? Yes. Recur on the function
;; passing new, oldL, oldR, (cdr lat), and the new col.
;;
;; 4. THIRD RECURSIVE STEP
;; new = foo
;; oldL = bar
;; oldR = baz
;; lat = (qux quux)
;; col = right-col
;;
;; - Is lat null? No.
;; - Is car of lat equal to oldL? No.
;; - Is car of lat equal to oldR? No.
;; - Else? Yes. Recur on the function passing new, oldL, oldR,
;; (cdr lat) and the new collector.
;;
;; 5. FOURTH RECURSIVE STEP
;; new = foo
;; oldL = bar
;; oldR = baz
;; lat = (quux)
;; col = else-col
;;
;; - Is the lat null? No.
;; - Is the car of lat equal to oldL? No.
;; - Is the car of lat equal to oldR? No.
;; - Else? Yes. Recur on the function passing new, oldL, oldR,
;; (cdr lat) and the new collector.
;;
;; 6. FIFTH RECURSIVE STEP
;; new = foo
;; oldL = bar
;; oldR = baz
;; lat = ()
;; col = else-col
;;
;; - Is the lat null? Yes. Call else-col, where newlat is (),
;; L is 0 and R is 0.
;;
;; 7. ELSE-COL
;; newlat = ()
;; L = 0
;; R = 0
;; col = else-col
;;
;; - Now call else-col on the (cons (car lat)), which is currently
;; stored in memory as the atom "quux", onto newlat, as well as
;; L and R.
;;
;; 8. ELSE-COL (again)
;; newlat = (quux)
;; L = 0
;; R = 0
;; col = right-col
;;
;; - Now call right-col on the (cons (car lat)), which is currently
;; stored in memory as the atom "qux", onto newlat, as well as L
;; and R.
;;
;; 9. RIGHT-COL
;; newlat = (qux quux)
;; L = 0
;; R = 0
;; col = left-col
;;
;; - Now call left-col on the cons of oldR and (cons new newlat),
;; as well as L and the increment of R.
;;
;; 10. LEFT-COL
;; newlat = (baz foo qux quux)
;; L = 0
;; R = 1
;; col = else-col
;;
;; - Now call else-col on the cons of new and (cons oldL newlat),
;; as well as the increment of L and R.
;;
;; 11. ElSE-COL (last time)
;; newlat = (foo bar baz foo qux quux)
;; L = 1
;; R = 1
;; col = list
;;
;; - Now we call list on the (cons (car lat)), which is currently
;; stored in memory as the atom "goo", onto newlat, as well as L
;; and R.
;; - This gives us the final, magical list:
;; ((goo foo bar baz foo qux quux) 1 1)
;;
;; 12. FIFTH RECURSIVE STEP (redux)
;; new = foo
;; oldL = bar
;; oldR = baz
;; lat = (quux)
;; col = else-col
;;
;; - Base case evaluated, with the result being
;; ((goo foo bar baz foo qux quux) 1 1).
;; - Function ends.
;;
;; THE END

关于lambda - 小阴谋家 : stuck on multiinsertLR&co example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12211874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com