如何计算重复序言
How to count duplicate prolog
我已经实现了这棵树(插入),但我想跟踪重复项,我该怎么做。
insert2(nil, nil, nil).
insert2(Info, nil, t((Info,1), nil, nil)).
insert2(Info , t((RootInfo,_), Left, Right),
t((RootInfo,_), LeftPlus, Right)) :-
Info< RootInfo,
insert2(Info, Left, LeftPlus).
insert2(Info , t((RootInfo,count), Left, Right),t((RootInfo,count+1), Left, Right)) :-
Info= RootInfo.
insert2(Info, t((RootInfo,_), Left, Right),
t((RootInfo,_), Left, RightPlus)) :-
RootInfo< Info,
insert2(Info, Right, RightPlus).
更清楚一点,这就是我的意思
-? T = t((20,3),t((10,1),nil,nil),t((30,2),nil,nil)),
insertT(10,T,NT).
NT = t((20,3), t((10,2),nil,nil),t((30,2),nil,nil)).
因为我已经插入了 10,树只会增加计数器而不是添加重复值。谢谢
既然你这么亲近,那我就收拾一下吧。 :)
您所拥有的唯一真正的问题是:
- 在其中一个子句中
Count
有一个原子而不是变量
- 您正在尝试使用 Prolog 添加 行 ,但实际上并不能这样。如果您尝试使用
Count + 1
作为参数,它将被读取为术语 +(Count, 1)
除非它被传递到可以评估它的谓词(如 is/2
或算术比较) .
- 您不需要第一个子句,
insert2(nil, nil, nil)
。或者至少如果您打算考虑插入 nil
,我认为结果应该是原始树,所以应该是 insert2(nil, Tree, Tree)
。您不会仅仅因为试图插入 nil
就使树无效。你会离开这棵树。
考虑这些因素:
% (3) Inserting nil into a Tree yields the same Tree
insert(nil, Tree, Tree).
% If you insert into an empty tree, the result is a tree with
% a single node consisting of your new info
insert(Info, nil, t((Info, 1), nil, nil)).
% (1) If you insert into a tree and the node at the top matches your
% Info, then you only increment the count of the top node
insert(Info, t((Info, C), L, R), t((Info, Cx), L, R)) :-
Cx is C + 1. % (2) `is/2` will evaluate; you can't do this "in-line"
% If you insert Info which is smaller than the top node value of
% a non-nil tree, then you insert the Info into the left branch
insert(Info, t((I, C), L, R), t((I, C), Lx, R)) :-
Info < I,
insert(Info, L, Lx).
% If you insert Info which is greater than the top node value of
% a non-nil tree, then you insert the Info into the right branch
insert(Info, t((I, C), L, R), t((I, C), L, Rx)) :-
Info > I,
insert(Info, R, Rx).
我已经实现了这棵树(插入),但我想跟踪重复项,我该怎么做。
insert2(nil, nil, nil).
insert2(Info, nil, t((Info,1), nil, nil)).
insert2(Info , t((RootInfo,_), Left, Right),
t((RootInfo,_), LeftPlus, Right)) :-
Info< RootInfo,
insert2(Info, Left, LeftPlus).
insert2(Info , t((RootInfo,count), Left, Right),t((RootInfo,count+1), Left, Right)) :-
Info= RootInfo.
insert2(Info, t((RootInfo,_), Left, Right),
t((RootInfo,_), Left, RightPlus)) :-
RootInfo< Info,
insert2(Info, Right, RightPlus).
更清楚一点,这就是我的意思
-? T = t((20,3),t((10,1),nil,nil),t((30,2),nil,nil)),
insertT(10,T,NT).
NT = t((20,3), t((10,2),nil,nil),t((30,2),nil,nil)).
因为我已经插入了 10,树只会增加计数器而不是添加重复值。谢谢
既然你这么亲近,那我就收拾一下吧。 :)
您所拥有的唯一真正的问题是:
- 在其中一个子句中
Count
有一个原子而不是变量 - 您正在尝试使用 Prolog 添加 行 ,但实际上并不能这样。如果您尝试使用
Count + 1
作为参数,它将被读取为术语+(Count, 1)
除非它被传递到可以评估它的谓词(如is/2
或算术比较) . - 您不需要第一个子句,
insert2(nil, nil, nil)
。或者至少如果您打算考虑插入nil
,我认为结果应该是原始树,所以应该是insert2(nil, Tree, Tree)
。您不会仅仅因为试图插入nil
就使树无效。你会离开这棵树。
考虑这些因素:
% (3) Inserting nil into a Tree yields the same Tree
insert(nil, Tree, Tree).
% If you insert into an empty tree, the result is a tree with
% a single node consisting of your new info
insert(Info, nil, t((Info, 1), nil, nil)).
% (1) If you insert into a tree and the node at the top matches your
% Info, then you only increment the count of the top node
insert(Info, t((Info, C), L, R), t((Info, Cx), L, R)) :-
Cx is C + 1. % (2) `is/2` will evaluate; you can't do this "in-line"
% If you insert Info which is smaller than the top node value of
% a non-nil tree, then you insert the Info into the left branch
insert(Info, t((I, C), L, R), t((I, C), Lx, R)) :-
Info < I,
insert(Info, L, Lx).
% If you insert Info which is greater than the top node value of
% a non-nil tree, then you insert the Info into the right branch
insert(Info, t((I, C), L, R), t((I, C), L, Rx)) :-
Info > I,
insert(Info, R, Rx).