提前致谢,并对我的帖子中的任何错误或任何混淆道歉。
我在"的第3.4.6节中的树遍历示例中遇到错误计算机编程的概念,技术和模型"。我正在使用 Oz / mozart2-2.0.0-alpha.0 (+ build.4091-slitaz-1.01.ova)。
我正在输入下面的程序和函数,就像书中一样(本书的两个版本,代码略有不同),并在我尝试执行时出错。我自己试过修理它们,但到目前为止无法到达任何地方。我将给出代码,执行语句,错误和测试树声明(这似乎是有效的,因为我可以执行{LookUp},{Insert}和{Delete}上面没有问题)下面。我认为有必要包括更多笔记,下面......
所以,我的问题是这些代码有什么问题,或者我如何使用它们或者使用我的树?我包含了一个函数和三个程序,所有这些都给出了完整的相同问题(也许有助于排除故障),但我认为解决方案对所有人来说都是一样的。
树声明在下面的最后一个。
代码 - >
%All of the below errors occur upon execution, not on definition of the proc or function
% version: VanRoyHaridi2003-book.pdf
% section 3.4.6 - "Trees","Tree Traversal", "Depth-first traversal
% p.159 (Adobe Reader p. 202 of 939)
declare
proc {DFS T}
case T
of leaf then skip
[] tree(Key Val L R) then
{DFS L}
{Browse Key#Val}
{DFS R}
end
end
{DFS S}
/*
%******************** Error: conditional failed *****************
%**
%** Missing else clause
%**
%** Matching: tree(key:250 left:tree(key:125 left:tree(key:60 left:tree(key:30 left:leaf right:leaf value:pyramid) right:tree(key:90 left:leaf right:leaf value:cube) value:sphere) right:tree(key:185 left:tree(key:155 left:leaf right:leaf value:wave) right:tree(key:215 left:leaf right:leaf value:plane) value:shadow) value:shape) right:tree(key:375 left:tree(key:315 left:tree(key:285 left:leaf right:leaf value:boing) right:tree(key:345 left:leaf right:leaf value:ring) value:tap) right:tree(key:435 left:tree(key:405 left:leaf right:leaf value:whoosh) right:tree(key:465 left:leaf right:leaf value:ping) value:boom) value:sounds) value:treetop)
%** in file "/mnt/host_folder/trees - traversals", line 39
%**
%** Call Stack:
%** procedure 'DFS' in file "/mnt/host_folder/trees - traversals", line 33, column 1, PC = -1279897415
%**--------------------------------------------------------------
*/
% version: CTMchapters1-3.pdf, "Tree Traversal" p.155 (Adobe Read p. 181 of 226)
% section 3.4.6.2, "Tree Traversal"
declare
proc {DFS T}
case T
of leaf then skip
[] tree(Key Val L R) then
{Browse Key#Val}
{DFS L}
{DFS R}
end
end
{DFS S}
% {DFS T} version two had the same error as the 1st version
% I know they are trivially different, but included it anyway... sorry
% Same page as {DFS T} in CTMchapters1-3.pdf (only).
declare
proc {DFSAccLoop T S1 ?Sn}
case T
of leaf then Sn=S1
[] tree(Key Val L R) then S2 S3 in
S2=Key#Val|S1
{DFSAccLoop L S2 S3}
{DFSAccLoop L S3 Sn}
end
end
fun {DFSAcc T} {Reverse {DFSAccLoop S nil $}} end
{Browse {DFSAcc S}}
/*
%******************** Error: conditional failed *****************
%**
%** Missing else clause
%**
%** Matching: tree(key:250 left:tree(key:125 left:tree(key:60 left:tree(key:30 left:leaf right:leaf value:pyramid) right:tree(key:90 left:leaf right:leaf value:cube) value:sphere) right:tree(key:185 left:tree(key:155 left:leaf right:leaf value:wave) right:tree(key:215 left:leaf right:leaf value:plane) value:shadow) value:shape) right:tree(key:375 left:tree(key:315 left:tree(key:285 left:leaf right:leaf value:boing) right:tree(key:345 left:leaf right:leaf value:ring) value:tap) right:tree(key:435 left:tree(key:405 left:leaf right:leaf value:whoosh) right:tree(key:465 left:leaf right:leaf value:ping) value:boom) value:sounds) value:treetop)
%** in file "/mnt/host_folder/trees - traversals", line 67
%**
%** Call Stack:
%** procedure 'DFSAccLoop' in file "/mnt/host_folder/trees - traversals", line 61, column 1, PC = -1239512161
%** procedure 'DFSAcc' in file "/mnt/host_folder/trees - traversals", line 70, column 1, PC = -1239496575
%** toplevel abstraction in line 1, column 0, PC = -1238883005
%**--------------------------------------------------------------
*/
% One of the versions of {LookUp X T} gives the same error.
% version: CTMchapters1-3.pdf, "Tree Traversal" p.152 (Adobe Read p. 178 of 226)
% section 3.4.6.2, "Storing information in trees"
declare
fun {Lookup X T}
case T
of leaf then notfound
[] tree(Y V T1 T2) then
if X<Y then {Lookup X T1}
elseif X>Y then {Lookup X T2}
else found(V) end
end
end
/*
%******************** Error: conditional failed *****************
%**
%** Missing else clause
%**
%** Matching: tree(key:horse left:tree(key:dog left:tree(key:cat left:leaf right:leaf value:chat) right:tree(key:elephant left:leaf right:leaf value:elephant) value:chien) right:tree(key:mouse left:tree(key:monkey left:leaf right:leaf value:singe) right:tree(key:tiger left:leaf right:leaf value:tigre) value:souris) value:cheval)
%** in file "/mnt/host_folder/Lookup Insert Delete", line 37
%**
%** Call Stack:
%** procedure 'Lookup' in file "/mnt/host_folder/Lookup Insert Delete", line 31, column 1, PC = -1241765194
%** toplevel abstraction in line 1, column 0, PC = -1241110606
%**--------------------------------------------------------------
*/
% the case version works, so I was suprised when the traversals using case are not working for me.
declare
fun {Lookup K T}
case T of leaf then notfound
[] tree(key:X value:V left:T1 right:T2) andthen X==K then found(V)
[] tree(key:X value:V left:T1 right:T2) andthen X<K then {Lookup K T2}
[] tree(key:X value:V left:T1 right:T2) andthen X>K then {Lookup K T1}
end
end
% There are two test trees here
declare
S=tree(key:250 value:treetop
left:tree(key:125 value:dash
left:tree(key:60 value:sphere
left:tree(key:30 value:pyramid left:leaf right:leaf)
right:tree(key:90 value:cube left:leaf right:leaf))
right:tree(key:185 value:shadow
left:tree(key:155 value:wave left:leaf right:leaf)
right:tree(key:215 value:plane left:leaf right:leaf)))
right:tree(key:375 value:hum
left:tree(key:315 value:tap
left:tree(key:285 value:boing left:leaf right:leaf)
right:tree(key:345 value:ring left:leaf right:leaf))
right:tree(key:435 value:boom
left:tree(key:405 value:whoosh left:leaf right:leaf)
right:tree(key:465 value:ping left:leaf right:leaf))))
T=tree(key:horse value:cheval
left:tree(key:dog value:chien
left:tree(key:cat value:chat left:leaf right:leaf)
right:tree(key:elephant value:elephant left:leaf right:leaf))
right:tree(key:mouse value:souris
left:tree(key:monkey value:singe left:leaf right:leaf)
right:tree(key:tiger value:tigre left:leaf right:leaf)))
{Browse S}
{Browse T}
答案 0 :(得分:2)
查看您的第一个程序:您的树声明是错误的,因为您可以看到编译器无法将您的树与案例结构中定义的任何子句匹配,例如,您尝试匹配某些内容的第一个程序这个表格(宣言的简化形式):
S=tree(key:250 value:treetop left:leaf right:leaf)
有这个条款:
of leaf then skip
[] tree(Key Val L R)
S
既不能与leaf
匹配也不能与第二个匹配,因为Key
无法与key:250
匹配,因此编译器会为您提供该错误。
您可以将树声明为:
S = tree(250 treetop tree(120 foo leaf leaf) tree(100 foo2 leaf leaf))
您现在可以相应地修复其他程序..
答案 1 :(得分:1)
问题来自于您未正确匹配树的事实。当你写
fun {Lookup X T}
case T
of leaf then notfound
[] tree(Y V T1 T2) then
% ...
end
end
tree(Y V T1 T2)
隐式转换为tree(1:Y 2:V 3:T1 4:T2)
。这是Oz提供的语法糖:如果在声明记录时没有指定功能,那么它们将隐式添加为从1开始增加整数。
要正确匹配树,您必须明确说明正确的功能:
fun {Lookup X T}
case T
of leaf then notfound
[] tree(key:Y value:V left:T1 right:T2) then
% ...
end
end