Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1.  
  2. intersect(A,B,C):- findall(X,(member(X,A),member(X,B)),C).
  3.  
  4. difference(A,B,C):- findall(X,(member(X,A),not(member(X,B))),C).
  5.  
  6. %----------------------- PRÁTICA DIFFERENCE LISTS --------------------------%
  7.  
  8. %tree2list(void,[]).
  9. %tree2list(bt(X,E,D),L) :- tree2list(E,L1), tree2list(D,L2), append(L1,[X],L3), append(L3,L2,L).
  10.  
  11. tree2list(T,L) :- tree2listdl(T,L-[]).
  12. tree2listdl(void,L-L).
  13. tree2listdl(bt(X,E,D),L-R):- tree2listdl(E,L-[X|R1]), tree2listdl(D,R1-R).
  14.  
  15. preorder(T,L):- preordert(T,L-[]).
  16. preordert(void,L-L).
  17. preordert(bt(A,E,D),[A|L]-R):-preordert(E,L-R1), preordert(D,R1-R).
  18.  
  19. postorder(T,L):- postordert(T,L-[]).
  20. postordert(void,L-L).
  21. postordert(bt(A,E,D),L-R) :- postordert(E, L-R1), postordert(D,R1-[A|R]).
  22.  
  23. %separate([o,x,o,x,x,x,o,x],L).
  24.  
  25. separate(L,L2) :- separatedl(L,L2-[]).
  26. separatedl([],L2-L2).
  27. separatedl([o|L],L2-R2) :- separatedl(L, L2-[o|R2]).
  28. separatedl([x|L],[x|L2]-R2):- separatedl(L, L2-R2).
  29.  
  30.  
  31. %----------------------------- PRÁTICA DCG -------------------------------%
  32.  
  33.  
  34. dec-->d,dec.
  35. dec-->d.
  36. d-->[0];[1];[2];[3];[4];[5];[6];[7];[8];[9].
  37.  
  38. even-->p.
  39. even-->p,even.
  40. p-->[0];[2];[4];[6];[8].
  41.  
  42. greet-->sn(_,_).
  43. sn(N,G)-->adj(N,G),n(N,G).
  44. sn(N,G)-->det(N,G),adj(N,G),n(N,G).
  45. adj(sing,femi)-->[boa].
  46. adj(sing,maxo)-->[bom].
  47. adj(plur,femi)-->[boas].
  48. adj(plur,maxo)-->[bons].
  49. det(sing,maxo)-->[muito].
  50. det(sing,femi)-->[muita].
  51. det(plur,maxo)-->[muitos].
  52. det(plur,femi)-->[muitas].
  53. n(sing,femi)-->[tarde];[noite].
  54. n(plur,femi)-->[tardes];[noites].
  55. n(sing,maxo)-->[dia].
  56. n(plur,maxo)-->[dias].
  57.  
  58. bet(N, M, K) :- N < M, K = N.
  59. bet(N, M, K) :- N == M, !, K = N.
  60. bet(N, M, K) :- N < M, N1 is N+1, bet(N1, M, K).
  61.  
  62. date-->seq.
  63. seq-->day,month(_).
  64. day-->[1];[2];[3];[4];[5];[6];[7];[8];[9];[10];[11];[12];[13];[14];[15];
  65. [16];[17];[18];[19];[20];[21];[22];[23];[24];[25];[26];[27];[28];[29];[30];[31].
  66. month(D)-->{D=<31},[jan];[mar];[may];[aug];[oct];[dec].
  67. month(D)-->{D=<30},[apr];[jun];[jul];[sep];[nov].
  68. month(D)-->{D=<28},[feb].
  69.  
  70. %--- b a* b* ---%
  71. s-->b1,a,b.
  72. b1-->[b].
  73. a-->[a],a.
  74. a-->[].
  75. b-->[b],b.
  76. b-->[].
  77.  
  78. btree(void).
  79. btree(bt(R,_,_)):- number(R).
  80. btree(bt(_,E,_)):- number(E), btree(E).
  81. btree(bt(_,_,D)):- number(D), btree(D).
  82.  
  83. inbtree(bt(A,_,bt(S,_,_)),A,S):-!.
  84. inbtree(bt(_,E,_),A,S):- inbtree(E,A,S),!.
  85. inbtree(bt(_,_,D),A,S):- inbtree(D,A,S),!.
  86.  
  87.  
  88. containAll([loc(X1,Y1),loc(X2,Y2)],H,V):-
  89. Hmin is min(X1,X2), Hmax is max(X1,X2),
  90. Vmin is min(Y1,Y2), Vmax is max(Y1,Y2),
  91. H is Hmax-Hmin, V is Vmax-Vmin.
  92. %containAll([loc(X1,Y1),loc(X2,Y2)|XYs],H,V).
  93.  
  94. listLoc([loc(X1,Y1)|XYs],Hfin,Vfin):-
  95. listH([loc(X1,_)|XYs],H),
  96. listV([loc(_,Y1)|XYs],V),
  97. min_in_list(H,Hmin),
  98. max_in_list(H,Hmax),
  99. min_in_list(V,Vmin),
  100. max_in_list(V,Vmax),
  101. Hfin is Hmax-Hmin,
  102. Vfin is Vmax-Vmin.
  103.  
  104.  
  105. listH([],[]).
  106. listH([loc(X,_)|Xs],[X|H]):-
  107. listH(Xs,H).
  108.  
  109. listV([],[]).
  110. listV([loc(_,Y)|Ys],[Y|H]):-
  111. listV(Ys,H).
  112. %----Minimun in a list----%
  113. min_in_list([Min],Min).
  114.  
  115. min_in_list([H,K|T],M) :-
  116. H =< K,
  117. min_in_list([H|T],M).
  118.  
  119. min_in_list([H,K|T],M) :-
  120. H > K,
  121. min_in_list([K|T],M).
  122.  
  123. %-----Maximum in list-----%
  124. max_in_list([Min],Min).
  125.  
  126. max_in_list([H,K|T],M) :-
  127. H >= K,
  128. max_in_list([H|T],M).
  129.  
  130. max_in_list([H,K|T],M) :-
  131. H < K,
  132. max_in_list([K|T],M).
  133.  
  134.  
  135. %--- example 1: [a] [a,b,c]
  136. %--- example 2: [not,a]
  137. %--- example 3: [a, not, b]
  138. %--- example 4: [and, b, not ,c]
  139. %--- example 5: errado! [b, not] , [not]
  140.  
  141. frase--> seq1.
  142. seq1--> simbolo. % frase([a,b,c],[]).
  143. seq1--> conj. % frase([not],[]).
  144. seq1--> c. % frase([not,a,or,b,and,f],[]).
  145. seq1--> c2. % frase([a,or,b,and,c],[]).
  146. simbolo--> ([a];[b];[c];[d];[e];[f]),simbolo.
  147. simbolo--> [].
  148. c--> conj,simbolo2,c.
  149. c--> [].
  150. c2--> simbolo2,c.
  151. conj--> [not];[and];[or].
  152. simbolo2--> [a];[b];[c];[d];[e];[f].
  153.  
  154. %------- input: T1= bt(....), T2= bt(....), intersect_trees()
  155.  
  156. intersect2(A,B,C):- setof(X, (member(X,A),member(X,B)), C).
  157.  
  158. list2tree2(void,[]).
  159. list2tree2(bt(X,E,D), L):-
  160. list2tree2(E,L1),
  161. list2tree2(D,L2),
  162. append(L1,[X],L3),
  163. append(L3,L2,L).
  164.  
  165. intersect_trees(T1,T2,L):-
  166. list2tree2(T1,L),
  167. list2tree2(T2,L2),
  168. intersect2(L,L2,L).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement