1

在此先感谢,并为我的帖子中的任何错误或任何令人困惑的地方道歉。

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

2 回答 2

2

查看您的第一个程序:您的树声明是错误的,因为您可以看到编译器无法将您的树与案例结构中定义的任何子句匹配,例如对于您尝试以这种形式匹配某些内容的第一个程序(您的声明的简化形式):

S=tree(key:250 value:treetop left:leaf right:leaf)

用这个条款:

of leaf then skip
[] tree(Key Val L R)

Scan't match both leafor with the second, as Keycan't match withkey:250所以编译器会给你那个错误。您可以将您的树声明为:

S = tree(250 treetop tree(120 foo leaf leaf) tree(100 foo2 leaf leaf))

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

于 2016-01-23T11:09:08.517 回答
1

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

fun {Lookup X T}
   case T
   of leaf then notfound
   [] tree(Y V T1 T2) then
      % ...
   end
end

thetree(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
于 2016-03-07T15:33:39.787 回答