Advertisement
ganryu

Pco2

May 17th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 9.12 KB | None | 0 0
  1. % Ejercicio 1a
  2. % Comprobar si una lista de elementos constituye un conjunto válido
  3. % Un conjunto es una lista de elementos que no contiene elementos repetidos
  4.  
  5. % pertenece(Lista, Elemento): true si Elemento esta en Lista
  6. pertenece([], _) :- fail.
  7. pertenece([E|_], E) :- !.
  8. pertenece([_|L], E) :- pertenece(L, E).
  9.  
  10. % esConjunto(Lista): true si Lista es un conjunto
  11. esConjunto([]) :- !.
  12. esConjunto([E|L]) :- not(pertenece(L, E)), esConjunto(L).
  13.  
  14. conjunto(C, []) :- esConjunto(C), !.
  15. conjunto(C, [H|L]) :- pertenece(C, H), conjunto(C, L).
  16.  
  17. % 1b. Determina si un elemento pertenece a un conjunto
  18. % perteneceAConjunto(Conjunto, Elemento): Elemento se encuentra en Conjunto y Conjunto es un Conjunto
  19. perteneceAConjunto(C, E) :- esConjunto(C), pertenece(C, E).
  20.  
  21. % 1c. Incorpora un elemento a un conjunto
  22. % insertarEnConjunto(C, E, CE): CE es el resultado de insertarEnConjunto E en C
  23.  
  24. insertarEnConjunto(C, E, C) :- perteneceAConjunto(C, E), !.
  25. insertarEnConjunto(C, E, C2) :- append(C, [E], C2).
  26.  
  27. % 1d. Unir dos conjuntos
  28. % unirConjuntos(C, C2, U): U es el resultado de la union entre C y C2
  29. unirConjuntos([], [], []) :- !.
  30. unirConjuntos([], C, C) :- esConjunto(C), !.
  31. unirConjuntos([H|T], C, U) :-
  32.     unirConjuntos(T, C, U2),
  33.     insertarEnConjunto(U2, H, U).
  34.  
  35. % 1e. Intersectar dos conjuntos
  36. % intersectarConjuntos(C, C2, I): I es la intersección entre C y C2
  37. intersectarConjuntos([], C, []) :- esConjunto(C), !.
  38.  
  39. intersectarConjuntos([H|T], C, I) :-
  40.     not(perteneceAConjunto(C, H)),
  41.     intersectarConjuntos(T, C, I),
  42.     !.
  43.  
  44. intersectarConjuntos([H|T], C, I) :-
  45.     intersectarConjuntos(T, C, I2),
  46.     insertarEnConjunto(I2, H, I).
  47.  
  48. % 1f. Calcular la diferencia entre dos conjuntos
  49. % diferencia(C, C2, D): D es la diferencia entre C2 y C
  50. diferencia(_, [], []) :- !.
  51.  
  52. diferencia(C, [H|T], D) :-
  53.     perteneceAConjunto(C, H),
  54.     diferencia(C, T, D),
  55.     !.
  56.  
  57. diferencia(C, [H|T], D) :-
  58.     diferencia(C, T, D2),
  59.     insertarEnConjunto(D2, H, D).
  60.  
  61. % 1h. Dada una lista de elementos con repeticiones, construir un conjunto que contenga todos los elementos de esa lista.
  62. % convertir(L, C): C es el conjunto formado con los elementos sin repeticiones de la lista L
  63.  
  64. convertir([], []) :- !.
  65. convertir([H|T], C) :- convertir(T, C2), insertarEnConjunto(C2, H, C).
  66.  
  67. % 5. Definir eval
  68.  
  69. % unionExclusiva(C, C2, U): U es el conjunto resultado de la unión exclusiva entre C y C2.
  70. unionExclusiva(C, C2, U) :-
  71.     diferencia(C, C2, D1),
  72.     diferencia(C2, C, D2),
  73.     unirConjuntos(D1, D2, U).
  74.  
  75. eval(E, E) :- esConjunto(E).
  76. eval(A+B, R) :- eval(A, R1), eval(B, R2), unirConjuntos(R1, R2, R).
  77. eval(A/B, R) :- eval(A, R1), eval(B, R2), unionExclusiva(R1, R2, R).
  78. eval(A-B, R) :- eval(A, R1), eval(B, R2), diferencia(R2, R1, R).
  79. eval(A*B, R) :- eval(A, R1), eval(B, R2), intersectarConjuntos(R1, R2, R).
  80.  
  81. % 8 Árbol
  82.  
  83. % esArbol(A): devuelve si A es un árbol
  84. esArbol(#).
  85. esArbol(t(_, B, C)) :- esArbol(B), esArbol(C).
  86.  
  87. % crearArbolVacio(X): crea un arbol vacío y lo retorna en X
  88. crearArbolVacio(#).
  89.  
  90. % insertar(A, V, A2): A2 es el resultado de ingresar el valor V en el árbol A
  91. insertar(#, V, t(V, #, #)).
  92. insertar(t(V, I, D), V, t(V, I, D)) :- !.
  93. insertar(t(R, I, D), V, t(R, I, D2)) :-
  94.     V > R,
  95.     !,
  96.     insertar(D, V, D2).
  97.  
  98. insertar(t(R, I, D), V, t(R, I2, D)) :-
  99.     V < R,
  100.     !,
  101.     insertar(I, V, I2).
  102.  
  103. anterior(V, t(V, I, #), I) :- !.
  104. anterior(V, t(P, I, D), t(P, I, D2)) :- anterior(V, D, D2).
  105.  
  106. % eliminar(A, V, A2): A2 es el árbol resultante de eliminar el nodo con el valor V del árbol A
  107. eliminar(#, _, #).
  108. eliminar(t(V, I, #), V, I) :- !.
  109. eliminar(t(V, #, D), V, D) :- !.
  110. eliminar(t(V, I, D), V, t(A, I2, D)) :- !, anterior(A, I, I2).
  111. eliminar(t(R, I, D), V, t(R, I2, D)) :- V < R, !, eliminar(I, V, I2).
  112. eliminar(t(R, I, D), V, t(R, I, D2)) :- V > R, !, eliminar(D, V, D2).
  113.  
  114. :- begin_tests(pertenece).
  115. test(pertenece) :- pertenece([1, 2, 3], 1).
  116. test(pertenece) :- pertenece([1, 2, 3], 2).
  117. test(pertenece) :- pertenece([1, 2, 3], 3).
  118. test(pertenece, [fail]) :- pertenece([], 1).
  119. test(pertenece, [fail]) :- pertenece([1, 2, 3], 4).
  120. :- end_tests(pertenece).
  121.  
  122. :- begin_tests(esConjunto).
  123. test(esConjunto) :- esConjunto([]).
  124. test(esConjunto) :- esConjunto([1]).
  125. test(esConjunto) :- esConjunto([1, 2, 3]).
  126. test(esConjunto, [fail]) :- esConjunto([1, 2, 1]).
  127. :- end_tests(esConjunto).
  128.  
  129. :- begin_tests(perteneceAConjunto).
  130. test(perteneceAConjunto) :- perteneceAConjunto([1, 2, 3], 1).
  131. test(perteneceAConjunto) :- perteneceAConjunto([1, 2, 3], 2).
  132. test(perteneceAConjunto) :- perteneceAConjunto([1, 2, 3], 3).
  133. test(perteneceAConjunto, [fail]) :- perteneceAConjunto([], 1).
  134. test(perteneceAConjunto, [fail]) :- perteneceAConjunto([1, 2, 3], 4).
  135. test(perteneceAConjunto, [fail]) :- perteneceAConjunto([1, 1, 2, 3], 1).
  136. test(perteneceAConjunto, [fail]) :- perteneceAConjunto([1, 2, 3, 4, 3], 1).
  137. :- end_tests(perteneceAConjunto).
  138.  
  139. :- begin_tests(insertarEnConjunto).
  140. test(insertarEnConjunto) :- insertarEnConjunto([], 1, [1]).
  141. test(insertarEnConjunto) :- insertarEnConjunto([1, 2, 3], 1, [1, 2, 3]).
  142. test(insertarEnConjunto) :- insertarEnConjunto([1, 2, 3], 2, [1, 2, 3]).
  143. test(insertarEnConjunto) :- insertarEnConjunto([1, 2, 3], 3, [1, 2, 3]).
  144. test(insertarEnConjunto) :- insertarEnConjunto([1, 2, 3], 4, [1, 2, 3, 4]).
  145. test(insertarEnConjunto) :-
  146.     E = 4,
  147.     insertarEnConjunto([1, 2, 3], E, [1, 2, 3, 4]).
  148. test(insertarEnConjunto) :-
  149.     C = [1, 2, 3],
  150.     insertarEnConjunto(C, 4, [1, 2, 3, 4]).
  151. :- end_tests(insertarEnConjunto).
  152.  
  153. :- begin_tests(unirConjuntos).
  154. test(unirConjuntos) :- unirConjuntos([], [], []).
  155.  
  156. test(unirConjuntos) :-
  157.     unirConjuntos([1, 2], [3, 4], L),
  158.     conjunto(L, [1, 2, 3, 4]).
  159.  
  160. test(unirConjuntos) :-
  161.     unirConjuntos([1, 2], [1, 2], L),
  162.     conjunto(L, [1, 2]).
  163.    
  164. test(unirConjuntos) :-
  165.     unirConjuntos([1, 2], [1, 2, 3], L),
  166.     conjunto(L, [1, 2, 3]).
  167.  
  168. test(unirConjuntos) :-
  169.     unirConjuntos([1, 2, 3, 4], [2], L),
  170.     conjunto(L, [1, 2, 3, 4]).
  171.  
  172. test(unirConjuntos) :-
  173.     unirConjuntos([1, 2, 3, 4], [2, 3, 4, 5], L),
  174.     conjunto(L, [1, 2, 3, 4, 5]).
  175.  
  176. :- end_tests(unirConjuntos).
  177.  
  178. :- begin_tests(intersectarConjuntos).
  179. test(intersectarConjuntos) :- intersectarConjuntos([], [], []).
  180.  
  181. test(intersectarConjuntos) :- intersectarConjuntos([1, 2], [3, 4], []).
  182.  
  183. test(intersectarConjuntos) :- intersectarConjuntos([1, 2], [1, 2], L),
  184.     conjunto(L, [1, 2]).
  185.    
  186. test(intersectarConjuntos) :-
  187.     intersectarConjuntos([1, 2], [1, 2, 3], L),
  188.     conjunto(L, [1, 2]),
  189.     not(conjunto(L, [1, 2, 3])).
  190.  
  191. test(intersectarConjuntos) :-
  192.     intersectarConjuntos([1, 2, 3, 4], [2], L),
  193.     conjunto(L, [2]),
  194.     not(perteneceAConjunto(L, 1)),
  195.     not(perteneceAConjunto(L, 3)),
  196.     not(perteneceAConjunto(L, 4)).
  197.  
  198. test(intersectarConjuntos) :-
  199.     intersectarConjuntos([1, 2, 3, 4], [2, 3, 4, 5], L),
  200.     conjunto(L, [2, 3, 4]),
  201.     not(perteneceAConjunto(L, 1)),
  202.     not(perteneceAConjunto(L, 5)).
  203. :- end_tests(intersectarConjuntos).
  204.  
  205. :- begin_tests(diferencia).
  206. test(diferencia) :- diferencia([], [], []).
  207.  
  208. test(diferencia) :-
  209.     diferencia([1, 2], [3, 4], L),
  210.     conjunto(L, [3, 4]),
  211.     not(perteneceAConjunto(L, 1)),
  212.     not(perteneceAConjunto(L, 2)).
  213.  
  214. test(diferencia) :- diferencia([1, 2], [1, 2], []).
  215.    
  216. test(diferencia) :-
  217.     diferencia([1, 2], [1, 2, 3], L),
  218.     perteneceAConjunto(L, 3),
  219.     not(perteneceAConjunto(L, 2)),
  220.     not(perteneceAConjunto(L, 1)).
  221.  
  222. test(diferencia) :- diferencia([1, 2, 3, 4], [2], []).
  223.  
  224. test(diferencia) :-
  225.     diferencia([1, 2, 3, 4], [2, 3, 4, 5], L),
  226.     perteneceAConjunto(L, 5),
  227.     not(perteneceAConjunto(L, 1)),
  228.     not(perteneceAConjunto(L, 2)),
  229.     not(perteneceAConjunto(L, 3)),
  230.     not(perteneceAConjunto(L, 4)).
  231. :- end_tests(diferencia).
  232.  
  233. :- begin_tests(convertir).
  234. test(convertir) :- convertir([], []).
  235. test(convertir) :-
  236.     convertir([1, 2, 3], C),
  237.     conjunto(C, [1, 2, 3]).
  238.  
  239. test(convertir) :-
  240.     convertir([1, 2, 3, 1, 2], C),
  241.     conjunto(C, [1, 2, 3]).
  242. :- end_tests(convertir).
  243.  
  244. :- begin_tests(eval).
  245. test(eval) :- eval([1,2,3], [1, 2, 3]).
  246. test(eval) :-
  247.     eval([1,2]+[3,4], A),
  248.     unirConjuntos([1, 2], [3, 4], A).
  249.  
  250. test(eval) :-
  251.     eval(([1,2]+[3,4])+[5,6], A),
  252.     unirConjuntos([1, 2], [3, 4], R),
  253.     unirConjuntos(R, [5, 6], A).
  254.  
  255. test(eval) :-
  256.     eval(([a,b,c,d,e]/[d,e,f,g,h])+([h,i,j,k,l]*([j,k,l,m,n,t,u]-[m,n])), A),
  257.     conjunto(A, [a,b,c,f,g,h,j,k,l]).
  258. :- end_tests(eval).
  259.  
  260. :- begin_tests(arbol).
  261. test(arbol) :-
  262.     crearArbolVacio(A),
  263.     insertar(A, 30, A2),
  264.     A2 == t(30, #, #).
  265.  
  266. test(arbol) :-
  267.     crearArbolVacio(A),
  268.     insertar(A, 30, A2),
  269.     insertar(A2, 15, A3),
  270.     A3 == t(30, t(15, #, #), #).
  271.  
  272. test(arbol) :-
  273.     crearArbolVacio(A),
  274.     insertar(A, 30, A2),
  275.     insertar(A2, 15, A3),
  276.     insertar(A3, 7, A4),
  277.     insertar(A4, 31, A5),
  278.     A5 == t(30, t(15, t(7, #, #), #), t(31, #, #)).
  279.  
  280. test(arbol) :-
  281.     crearArbolVacio(A),
  282.     insertar(A, 30, A2),
  283.     insertar(A2, 15, A3),
  284.     insertar(A3, 15, A4),
  285.     insertar(A4, 31, A5),
  286.     A5 == t(30, t(15, #, #), t(31, #, #)).
  287.  
  288. test(arbol) :-
  289.     crearArbolVacio(A),
  290.     insertar(A, 30, A2),
  291.     insertar(A2, 15, A3),
  292.     insertar(A3, 7, A4),
  293.     insertar(A4, 31, A5),
  294.     eliminar(A5, 7, A6),
  295.     A6 == t(30, t(15, #, #), t(31, #, #)).
  296.  
  297. test(arbol) :-
  298.     crearArbolVacio(A),
  299.     insertar(A, 30, A2),
  300.     insertar(A2, 15, A3),
  301.     insertar(A3, 7, A4),
  302.     insertar(A4, 31, A5),
  303.     eliminar(A5, 15, A6),
  304.     A6 == t(30, t(7, #, #), t(31, #, #)).
  305. :- end_tests(arbol).
  306. :- run_tests.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement