may(_,[],[]).
may(num(U),[est(C1,N1,NT1)|T1],[est(C1,N1,NT1)|T2]):-
U =< NT1,
may(num(U),T1,T2).
min(_,[],[]).
min(num(U),[est(C2,N2,NT2)|T3],[est(C2,N2,NT2)|T4]):-
U > NT2,
min(num(U),T3,T4).
main:-
U is 2.0,
mayores(num(U),
[ est(3,"J",3.1), est(6,"P",4.5), est(7,"L",2.0), est(4,"R",1.5),
est(2,"C",4.7), est(5,"F",2.0), est(9,"A",3.5), est(11,"K",4.8),
est(8,"M",2.4), est(15,"S",1.5), est(17,"D",0.5), est(19,"G",2.0)
],
T2),
menores(num(U),
[ est(3,"J",3.1), est(6,"P",4.5), est(7,"L",2.0), est(4,"R",1.5),
est(2,"C",4.7), est(5,"F",2.0), est(9,"A",3.5), est(11,"K",4.8),
est(8,"M",2.4), est(15,"S",1.5), est(17,"D",0.5), est(19,"G",2.0)
],
T4),
write(T4),
write(T2),
halt.
:- main.
代码需要分为不同的列表值&lt; = 2和值> 2。但是当我将其插入IDEONE时,它给了我这个错误。
Goal (directive) failed: user:main.
首先,更喜欢在程序中使用Toplevel代替halt/0
或write/1
。因此:
t2_t4(T2, T4) :-
Es = [ est(3,"J",3.1), est(6,"P",4.5), est(7,"L",2.0), est(4,"R",1.5),
est(2,"C",4.7), est(5,"F",2.0), est(9,"A",3.5), est(11,"K",4.8),
est(8,"M",2.4), est(15,"S",1.5), est(17,"D",0.5), est(19,"G",2.0)
],
U is 2.0,
mayores(num(U), Es, T2),
menores(num(U), Es, T4).
?- t2_t4(T2, T4).
false.
不幸的是,这失败了。为了减少故障源,请减少数据大小和目标数量。我完成了:
: - OP(950,FY, *(。*(_(。Menores(_,[],_/*[]*/(。menores(num(u(,[EST(C2,N2,NT2(| T3],_/*[EST(C2,N2,NT2(| T4]*/(: - u> nt2, *menores(num(u(,t3,t4(。T2_T4(T2,T4(: - es = [/*est(3," j",3.1(,EST(6," P",4.5(,EST(7," L",2.0(,EST(4," R",1.5(,est(2," C",4.7(,EST(5," F",2.0(,EST(9," A",3.5(,EST(11," K",4.8(,est(8," M",2.4(,EST(15," S",1.5(,EST(17," D",0.5(,*/ EST(19," G",2.0( ],, u是2.0, *mayores(num(u(,es,t2(, Menores(NUM(U(,ES,T4(。
因此,实际问题是menores
仅针对元素较小的情况定义。如果它们相等或更高的menores
失败。您也需要明确说明这一点。mayores
的同样参数。为什么不将两者都放入一个谓词中?另外,无需使用num/1
。
mayores_menores(_,[], [], []).
mayores_menores(U,[E|Es],[E|Mays],Mens):-
E = est(_,_,NT),
U =< NT,
mayores_menores(U,Es,Mays,Mens).
mayores_menores(U,[E|Es],Mays,[E|Mens]):-
E = est(_,_,NT),
U > NT,
mayores_menores(U,Es,Mays,Mens).
t2_t4x(T2, T4) :-
Es = [ est(3,"J",3.1), est(6,"P",4.5), est(7,"L",2.0), est(4,"R",1.5),
est(2,"C",4.7), est(5,"F",2.0), est(9,"A",3.5), est(11,"K",4.8),
est(8,"M",2.4), est(15,"S",1.5), est(17,"D",0.5), est(19,"G",2.0)
],
mayores_menores(2.0, Es, T2, T4).