Mozart / Oz 中的错误以及“计算机编程的概念、技术和模型”一书中的树遍历示例
Errors in Mozart / Oz with tree traversal examples from book " Concepts, Techniques, and Models of Computer Programming "
提前致谢,对于我的 post 的任何错误或任何令人困惑的事情,我们深表歉意。
我在“计算机编程的概念、技术和模型”的第 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}
查看您的第一个程序:您对树的声明是错误的,因为您可以看到编译器无法将您的树与 case 结构中定义的任何子句相匹配,例如您尝试匹配的第一个程序这种形式的东西(声明的简化形式):
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
提前致谢,对于我的 post 的任何错误或任何令人困惑的事情,我们深表歉意。
我在“计算机编程的概念、技术和模型”的第 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}
查看您的第一个程序:您对树的声明是错误的,因为您可以看到编译器无法将您的树与 case 结构中定义的任何子句相匹配,例如您尝试匹配的第一个程序这种形式的东西(声明的简化形式):
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