莫扎特/奥兹中的错误与第 " Concepts, Techniques, and Models of Computer Programming " 卷中的树遍历示例



提前致谢,对于我的帖子的任何错误或任何令人困惑的事情,我们深表歉意。

我在"计算机编程的概念、技术和模型"的第 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}

看看你的第一个程序:你的树声明是错误的,正如你所看到的,编译器不能将你的树与大小写结构中定义的任何子句匹配,例如,对于第一个程序,你试图匹配这种形式的内容(声明的简化形式):

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))

您现在可以相应地修复其他程序。

问题来自您没有正确匹配树的事实。当你写的时候

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

相关内容

最新更新