Last active
January 1, 2016 02:58
-
-
Save sacdallago/8082287 to your computer and use it in GitHub Desktop.
Prolog Project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Exercise one, write code to veryfiy second list is duplicate of first and presents same char at end of first list twice. | |
% ---------------------------------------------------------------------------------------------------------------FINISHED. | |
duplicar_ultimo([],[]). | |
duplicar_ultimo(L1,L2):-duplicar_ultimo_accessory(L1,L2). | |
duplicar_ultimo_accessory([X],[X,X]). | |
duplicar_ultimo_accessory([X|L1],[X|L2]):-duplicar_ultimo_accessory(L1,L2). | |
% Exercise two, code to verifiy palindrome. Can check by veryfying that reverse of a list is equal to itself; need method for reverse; need accessory method for reverse to actually perform inversion (as PROLOG reads front-end). | |
% ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------FINISHED. | |
palindromo(X):-inverse(X, X). | |
inverse([],[]). | |
inverse([X|L1],Z):-inverse_accessory(L1, [X], Z). | |
inverse_accessory([], L, L). | |
inverse_accessory([X|L1], L2, W):-inverse_accessory(L1, [X|L2], W). | |
% Exercise three, create a method to insert a number X in a list of numbers L, such that X is in the right ascending position (i.e. insert 4 into [1,2,5] -> [1,2,4,5]). | |
% -------------------------------------------------------------------------------------------------------------------------------------------------------------FINISHED. | |
insertarOrden([],X,[X]). | |
insertarOrden([A|L],X,[X,A|L]) :- A >= X, !. | |
insertarOrden([A|L1],X,[A|L2]):- insertarOrden(L1,X,L2). | |
% Exercise four, eliminate second same char from list (repetition). | |
% --------------------------------------------------------FINISHED. | |
eliminar_repetidos([],[]). | |
eliminar_repetidos([E|L1],[E|L2]) :- eliminar(E,L1,NL), eliminar_repetidos(NL,L2). | |
eliminar(_,[],[]). | |
eliminar(X,[X|L1],NL) :- eliminar(X,L1,NL). | |
eliminar(X,[E|L1],[E|NL]) :- not(X = E), eliminar(X,L1,NL). | |
% Exercise five, create all possible combinations of the first list. | |
% --------------------------------------------------------FINISHED. | |
combinacion([],[]). | |
combinacion(L1,[A|L2]):- combinacion_accessory(A,L1,L3), combinacion(L3,L2). | |
combinacion_accessory(X,[X|L],L). | |
combinacion_accessory(X,[E|L1],[E|L2]):- combinacion_accessory(X,L1,L2). | |
% Exercise six, PART A, TODAS_ALTERNADAS: Create a list of all output of alternar. | |
% -----------------------------------------------------------------------FINISHED. | |
todas_alternadas([],[]). | |
todas_alternadas(L1,[L3|L2]):- alternar(L1,L3), !, todalt([L3],L2). | |
todalt([E|L],[NL|L2]):- alternar(E,NL), not(equalList(NL,[E|L])), !, todalt([E,NL|L],L2). | |
todalt(_,[]). | |
equalList(L,[L|_]):-!. | |
equalList(L,[_|L2]):-equalList(L,L2). | |
% Exercise six, PART B, ALTERNAR: Create combination of elements, but filter in the manner that a triple of elements must have the central element either bigger or smaller then the other 2. | |
% -----------------------------------------------------------------------------------------------------------------------------------------------------------------------FINISHED. | |
alternar([],[]). | |
alternar(L1,L2):- combinacion(L1,L2), alternarBounds(L2). | |
alternarBounds([_,_]). | |
alternarBounds([A,B,C|L]):- alternarBounds([B,C|L]), A < B, C < B, !. | |
alternarBounds([A,B,C|L]):- alternarBounds([B,C|L]), A > B, C > B, !. | |
% Exercise seven, defined a constant, read from a list and if there is a sequence of at lest constant time a random char, then add that char as many times as it is repeated. | |
% ------------------------------------------------------------------------------------------------------------------------------------------------------------------FINISHED. | |
filtrar_secuencias(_,[],[]). | |
filtrar_secuencias(X,L1,L):- createRepeatedElementList(L1,REL,Y), Y<X, createNewSearchList(L1,REL,L3), filtrar_secuencias(X,L3,L), !. | |
filtrar_secuencias(X,L1,L4):- createRepeatedElementList(L1,REL,Y), Y>=X, createNewSearchList(L1,REL,L3), mergeLists(REL,L2,L4), filtrar_secuencias(X,L3,L2), !. | |
createRepeatedElementList([A],[A], 1). | |
createRepeatedElementList([A,B|_],[A], 1):- A \== B. | |
createRepeatedElementList([A,A],[A,A], 2). | |
createRepeatedElementList([A,A|L1],[A|L3],X):- createRepeatedElementList([A|L1],L3,X1), X is X1+1. | |
createNewSearchList(L,[],L). | |
createNewSearchList([A|L1],[A|L2],L3):- createNewSearchList(L1,L2,L3). | |
mergeLists([],[],[]). | |
mergeLists([],[A|L2],[A|L3]):-mergeLists([],L2,L3). | |
mergeLists([A|L1],L2,[A|L3]):-mergeLists(L1,L2,L3). | |
% Exercise eight, a mess. | |
% --------------FINISHED. | |
%just add if X coor is not X coor of P, in the end the list L1 is gonna be empty, therefore the base case will be executed. | |
insertar([],(A,AL),[(A,[AL])]). | |
insertar([(E,EL)|L1],(A,AL),[(E,EL)|L2]):- A \== E, insertar(L1,(A,AL),L2). | |
insertar([(A,EL)|L1],(A,AL),[(A,NL)|L1]):-combineLists(EL,AL,NT), eliminar_repetidos(NT,NL). | |
combineLists([X],L,[X,L]). | |
combineLists([A|EL],AL,[A|NL]):- combineLists(EL,AL,NL). | |
% Exercise nine, a mess. | |
% -------------FINISHED. | |
agrupar_coordenadas([],[]). | |
agrupar_coordenadas([(A,B)],L):- insertar([],(A,B),L), !. | |
agrupar_coordenadas([(A,B)|L1],[(A,LA)|L2]):- A == A, searchElement(L1,A,B,TL), eliminar_repetidos(TL,TA),mysort(TA,LA), eliminate_sequence(L1,L3,A), agrupar_coordenadas(L3,L2), !. | |
searchElement([],_,B,[B]). | |
searchElement([(E,_)|L1],A,B,L2):- A \== E, searchElement(L1,A,B,L2). | |
searchElement([(A,C)|L1],A,B,[C|L2]):- searchElement(L1,A,B,L2). | |
eliminate_sequence([],_,_). | |
eliminate_sequence([(A,_)|L1],NL,A) :- eliminate_sequence(L1,NL,A). | |
eliminate_sequence([(E,EL)|L1],[(E,EL)|NL],A) :- not(A = E), eliminate_sequence(L1,NL,A). | |
mysort(OL,NL):- insertionSort(OL,[],NL). | |
insertionSort([],L,L). | |
insertionSort([E|OL],L,NL):-insertarOrden(L,E,NNL),insertionSort(OL,NNL,NL). | |
% Exercise ten, a mess. | |
% ------------FINISHED. | |
%-----------------------% | |
par(X):- (X mod 2) =:= 0. | |
%-----------------------% | |
filtrar(_, [], _, []). | |
filtrar(L1, [A|L2], B, [E|L3]):-findElement(L1,A,E),executePredicate(E,B),filtrar(L1, L2, B, L3), !. | |
filtrar(L1, [_|L2], B, L3):-filtrar(L1, L2, B, L3). | |
findElement([Element|_],N,Element):- N =:= 1, !. | |
findElement([_|List],N,E) :- findElement(List,N-1,E). | |
executePredicate(X,Y):- T=..[Y,X], T. | |
end_of_file. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment