目标(指令)失败:用户:主Prolog swi

时间:2017-04-18 23:46:29

标签: prolog prolog-toplevel

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和值&gt; 2分成不同的列表。但当我把它运行到ideone时,它给了我这个错误。

Goal (directive) failed: user:main.

1 个答案:

答案 0 :(得分:4)

首先,您更喜欢在程序中使用顶层代替halt/0write/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 is 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).