我的一个谓词给了我一个像这样的列表作为输出:
WITH T
AS (SELECT *,
prev = LAG(direction) OVER (ORDER BY ID),
rn = ROW_NUMBER() OVER (ORDER BY ID)
FROM #data)
SELECT mytime,
Distance,
Direction,
SUM(CASE WHEN rn > 1 AND EXISTS(SELECT prev
INTERSECT
SELECT Direction) THEN 0 ELSE 1 END) OVER (ORDER BY id) AS GroupNumber
FROM T
ORDER BY ID
当具有[m(1,2,[v(1,y),v(1,y)]),m(1,6,[v(1,y),v(5,x)]),m(1,4,[v(3,x),v(1,y)]),m(1,8,[v(3,x),v(5,x)])]
元素的子列表中的符号相等时(如此处v
):
y
或[v(1,y),v(1,y)]
此处:
x
我必须对左边的数字求和并重建列表,如下所示:
[v(3,x),v(5,x)]
我有一个类似的谓词可以在主列表上运行,但我没有把这个正确。
这是另一个有效的谓词:
[m(1,2,[v(2,y)]),m(1,6,[v(1,y),v(5,x)]),m(1,4,[v(3,x),v(1,y)]),m(1,8,[v(8,x)])]
它以这种方式行事:
simplify([], []) :- !.
simplify([X], [X]) :- !.
simplify([m(C, TD, Var), m(C2, TD, Var)| Xs], Ys) :-
sumxp(C, C2, K), simplify([m(K, TD, Var)|Xs], Ys), !.
simplify([X, Y|Xs], [X|Ys]) :- !, simplify([Y|Xs], Ys).
sumxp(Power1, Power2, TotalPower) :- TotalPower is Power1 + Power2.
答案 0 :(得分:1)
simplify_01
规则适用于列表中的每个项目,例如m(1,6,[v(1,y),v(5,x)])
simplify_01(m(A,B,[v(X,Var1),v(Y,Var2)]),m(A,B,[v(X,Var1),v(Y,Var2)])) :-
Var1 \= Var2.
simplify_01(m(A,B,[v(X,Var1),v(Y,Var1)]),m(A,B,[v(Z,Var1)])) :-
Z is X + Y.
并且maplist
只会将simplify_01
应用于列表中的所有项目。
simplify(L,R) :-
maplist(simplify_01,L,R).
使用SWI-Prolog
运行时simplify([m(1,2,[v(1,y),v(1,y)]),m(1,6,[v(1,y),v(5,x)]),m(1,4,[v(3,x),v(1,y)]),m(1,8,[v(3,x),v(5,x)])],R).
R = [m(1, 2, [v(2, y)]), m(1, 6, [v(1, y), v(5, x)]), m(1, 4, [v(3, x), v(1, y)]), m(1, 8, [v(8, x)])] ;
false.