在完成一些工作之后,我能够理解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
,所以我使用了第138页和第140页中解释的a-friend
收集器和last-friend
收集器,分别。使用任一收集器评估函数会导致以下错误(使用带有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.
我多次查看代码,但找不到错误。如果有人有任何见解,请分享。如果有人也可以通过一些好的例子指出我(相对而言)对延续的温和解释,那么也会非常感激。
答案 0 :(得分:2)
以下是我解决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
答案 1 :(得分:1)
如代码中的基本情况所示,您的收集器/继续必须采用3个参数。看来你传入了一个没有的函数。
你通过multirember&co
工作很好:我写了一个关于它的答案,which you may wish to review。
无论如何,测试这些收集器/延续的一般方法是首先使用list
作为延续,因为list
是可变参数并且只返回作为列表给出的项目。这是一个例子:
> (multiinsertLR&co 'foo 'bar 'baz '(foo bar baz qux quux) list)
'((foo foo bar baz foo qux quux) 1 1)
哦,我看到两个foo
了!插入哪一个,哪一个在原始列表中?
> (multiinsertLR&co 'foo 'bar 'baz '(goo bar baz qux quux) list)
'((goo foo bar baz foo qux quux) 1 1)
啊,所以我们正在做点什么!只要列表中包含bar
,就会在其前插入foo
;只要列表中包含baz
,就会在其后插入foo
。但数字呢?
> (multiinsertLR&co 'foo 'bar 'baz '(baz goo bar baz qux bar quux baz) list)
'((baz foo goo foo bar baz foo qux foo bar quux baz foo) 2 3)
这些数字看起来像柜台!每个“左”插入增加第一个计数器,每个“右”插入增加第二个计数器。
在查看代码时,如果查看cond
的每个分支,您将看到在左匹配情况,右匹配情况和不匹配情况下发生的情况(当然还有一个列表结尾的情况,它设置基数/初始值)。注意在每种情况下如何发生插入(和计数器增量)。由于您已经获得multirember&co
,因此从现在开始这应该相当容易。一切顺利!
答案 2 :(得分:0)
再次用我最喜欢的模式匹配伪代码重写这个, 1 我们得到的更紧凑,视觉上更明显
multiinsertLR&co new oldL oldR lat col = g lat col
where
g [a, ...t] col
| a == oldL = g t ( newlat L R => col [new, oldL, ...newlat] (add1 L) R )
| a == oldR = g t ( newlat L R => col [oldR, new, ...newlat] L (add1 R) )
| else = g t ( newlat L R => col [a, ...newlat] L R )
g [] col = col [] 0 0
所以L
是oldL
中lat
的计数; R
是oldR
中lat
的计数; new
是一个特殊元素,插入到正在构建的结果列表中,位于输入list-of-atoms {oldR
右侧和oldL
元素的左侧{ {1}}:
lat
就是这样。有用的符号,很容易。
1 请参阅和this。