如何折射显示功能?

时间:2014-03-21 17:04:39

标签: prolog

我有这些显示功能:

display_siblings(X,Y):-
     setof(X-Y, (siblings(X,Y), ordered(X-Y)), Sibs),
     write('The siblings of'),
            write(X),  
            write('are'),nl, 
            foreach(member(Pair, Sibs), 
               writeln(Pair)),nl, 
        write('% end'), nl.

display_sisters(X,Y):-
     setof(X-Y, (sisters(X,Y), ordered(X-Y)), Sibs),
     write('The sisters of'),
           write(X),  
           write('are'),nl, 
           foreach(member(Pair, Sibs), 
              writeln(Pair)),nl, 
       write('% end'), nl.

正如您所看到的,只有setof的第二个参数正在发生变化。 我可以制作两种显示功能,如果可以,我该怎么做?

鲁洛夫

1 个答案:

答案 0 :(得分:0)

一种方法是:

display_siblings(X, Y):-
     setof(X-Y, (siblings(X, Y), ordered(X-Y)), Sibs),
     write('The siblings of'),
     write_sibs(X, Sibs).

display_sisters(X, Y) :=
     setof(X-Y, (sisters(X, Y), ordered(X-Y)), Sibs),
     write('The sisters of'),
     write_sibs(X, Sibs).

write_sibs(X, Sibs):-
     write(X),  
     write('are'), nl, 
     foreach(member(Pair, Sibs), 
         writeln(Pair)),nl, 
     write('% end'), nl.

另一种方式是:

display_siblings(X, Y) :-
    display('siblings', X, Y).

display_sisters(X, Y) :-
    display('sisters', X, Y).

display(Type, X, Y) :-
    setof(X-Y, (call(Type, X, Y), ordered(X-Y)), Sibs),
    write('The '), write(Type), write(' of'),
    write(X),  
    write('are'),nl, 
    foreach(member(Pair, Sibs), 
        writeln(Pair)),nl, 
    write('% end'), nl.