Advertisement
PonaFly

3-4 prolog

Oct 11th, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.86 KB | None | 0 0
  1. :- initialization(consult('kinship.pro')).
  2.  
  3. pred(X,Y) :- (var(X) ; var(Y)), parent(X,Y).
  4. pred(X,Y) :- nonvar(X), nonvar(Y), parent(X,Y), !.
  5. pred(X,Y) :- parent(X,Z), pred(Z,Y).
  6.  
  7. bs(X,Y):-
  8. setof(L,parent(L,X),Lst),
  9. setof(K,parent(K,Y),Lst),
  10. X \=Y .
  11.  
  12. brother(X,Y):-
  13. male(X),
  14. setof(L,parent(L,X),Lst),
  15. setof(K,parent(K,Y),Lst),
  16. X \=Y .
  17.  
  18. sister(X,Y):-
  19. female(X),
  20. setof(L,parent(L,X),Lst),
  21. setof(K,parent(K,Y),Lst),
  22. X \=Y .
  23.  
  24. married(X,Y):-
  25. parent(X,Z),parent(Y,Z),
  26. X \= Y,!.
  27.  
  28. husband(X,Y):-
  29. married(X,Y),
  30. male(X).
  31.  
  32. cousin(X,Y):-
  33. setof(L,parent(L,X),[H1,H2|T]),
  34. setof(K,parent(K,Y),[H3,H4|T1]),
  35. (bs(H1,H3);bs(H1,H4);bs(H2,H3);bs(H2,H4)).
  36.  
  37. childrens(X,N):-
  38. setof(L,parent(X,L),Lst),
  39. length(Lst,N).
  40.  
  41. nephews(Lst,X):-
  42.  
  43. setof(L,(cousin(X,Y),parent(Y,L)),Lst).
  44.  
  45.  
  46. family(X):-
  47. X=[H1,H2|T],
  48. married(H1,H2),
  49. setof(L,parent(H1,L),Lst),
  50. Lst=T.
  51.  
  52. %n1 04-06 comp
  53.  
  54. comp(El1, El2, Order) :-  
  55.   atom(Order),
  56.  
  57.   Term =.. [Order, El1, El2],
  58.   call(Term).
  59.  
  60.  
  61.  %filter
  62.  %фильтруем первый
  63. filter(Lst1,Lst2,FiltFunc):- %EMPTY l1,l2=TRUE
  64. Lst1=[],Lst2=[],!.
  65. filter(Lst1,Lst2,FiltFunc):-  %false if
  66. ((Lst1\=[],Lst2=[]);(Lst1=[],Lst2\=[]),fail).
  67.  
  68. filter([H1|T1],Lst2,FiltFunc):- %запускаем терм каждый раз и при его выполнении и совпадении элементов
  69. Lst2=[H2|T2],                   %продолжаем
  70. atom(FiltFunc),
  71. Term=..[FiltFunc,H1],
  72. (
  73. (call(Term),H1=H2,!,filter(T1,T2,FiltFunc)) ;
  74. filter(T1,Lst2,FiltFunc)
  75. ).
  76.  
  77.  
  78. %addhead
  79. addhead(E,Lst1,Lst2):-
  80. (Lst2=[],Lst1=[],!).
  81.  
  82. addhead(E,Lst1,Lst2):-
  83. Lst1=[H1|T1],
  84. Lst2=[H2|T2],
  85. [E|H1]=H2,
  86. addhead(E,T1,T2).
  87.  
  88.  
  89. %в качестве  функции  можно подать предикат m
  90. foldl(X0,_,[],Res):-
  91. Res=X0,!.
  92. foldl(X0,F,Lst,Res):-
  93. Lst=[H|T],
  94. call(F,X0,H,K),
  95. foldl(K,F,T,Res).
  96.  
  97. m(A,B,C):-
  98. C is (A-B).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement