2
votes
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.

The code needs to separate into different list the values <=2 and the values >2. but it gave me this error when i run it into ideone.

Goal (directive) failed: user:main.
1
You are using mayores and may ...false

1 Answers

4
votes

First of all, prefer to use the toplevel in place of halt/0 or write/1 in your programs. Thus:

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.

Unfortunately, this fails. To reduce the source of failure, reduce the size of data and the number of goals. I finished at:

:- 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).

So the actual problem is that menores is only defined for the case where the elements are smaller. If they are equal or greater menores fails. You need to state that explicitly, too. Same argument for mayores. Why not put both into a single predicate? Also, there is no need to use 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).