Created
December 19, 2012 11:36
-
-
Save ivpusic/4336091 to your computer and use it in GitHub Desktop.
This file contains 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
% Duplicate elements in list % | |
dupl([], []). | |
dupl([G | R], [G | [G | Res]]):- | |
dupl(R, Res). | |
% --------------------------- % | |
% Duplicate n times elements in list % | |
dupln(L, N, R):- | |
duplin(L, N, R, N). | |
duplin([], _, [], _). | |
duplin([G | R], N, L, 0):- | |
duplin(R, N, L, N). | |
duplin([G | R], N, [G | R1], N1):- | |
N2 is N1 - 1, | |
duplin([G | R], N, R1, N2). | |
% ------------------------------------ % | |
% Drop every N'th element from a list % | |
drop(L, N, R):- | |
drop(L, N, R, N). | |
drop([], _, [], _). | |
drop([G | R], N, R1, 1):- | |
drop(R, N, R1, N). | |
drop([G | R], N, [G | R1], N1):- | |
N1 > 1, | |
N2 is N1 - 1, | |
drop(R, N, R1, N2). | |
% ------------------------------------ % | |
% Split elements in list by a given number % | |
split([], _, [], []). | |
split([G | R], 0, L1, [G | R1]):- | |
split(R, 0, L1, R1). | |
split([G | R], N, [G | R1], L2):- | |
N > 0, | |
N1 is N - 1, | |
split(R, N1, R1, L2). | |
% ---------------------------------------- % | |
% Slice elements from list % | |
slice([G | _], 1, 1, [G]). | |
slice([G | R], 1, E, [G | R1]):- | |
E1 is E - 1, | |
slice(R, 1, E1, R1). | |
slice([ G | R], B, E, L1):- | |
B1 is B - 1, | |
E1 is E - 1, | |
slice(R, B1, E1, L1). | |
% ------------------------ % | |
% Remove k-th element from list % | |
remove([G | R], 1, R, G). | |
remove([G | R], K, [G | R1], N):- | |
K1 is K - 1, | |
remove(R, K1, R1, N). | |
% ----------------------------- % | |
% Insert element at n-th position on list % | |
insert(El, L, 1, [El | L]). | |
insert(El, [G | R], P, [G | Res]):- | |
P1 is P - 1, | |
insert(El, R, P1, Res). | |
% --------------------------------- % | |
% Create list with numbers in given range % | |
range(X, X, [X]). | |
range(X, Y, [X | R1]):- | |
X1 is X + 1, | |
range(X1, Y, R1). | |
% --------------------------------------- % | |
% Find n-th element of list % | |
nth([G | _], 1, G). | |
nth([G | R], N, E):- | |
N1 is N - 1, | |
nth(R, N1, E). | |
% ----------------------------------------- % | |
% ------------------------ KOLOKVIJ ZADACI -------------------------- % | |
% Zadatak 1 % | |
pomnozi([], _, []). | |
pomnozi([G | R], P, Res):- | |
pomnozi(R, P, Res1), | |
Result is G * P, | |
Res = [Result | Res1]. | |
% Zadatak 2 % | |
zbroji_l([], [], []). | |
zbroji_l([G | R], [G1 | R1], Res):- | |
zbroji_l(R, R1, L3), | |
Re is G + G1, | |
Res = [Re | L3]. | |
% Zadatak 3 % | |
manji_od([], _, []). | |
manji_od([G | R], N, Res):- | |
manji_od(R, N, Res1), | |
( | |
G < N | |
-> Res = [G | Res1] | |
; | |
Res = Res1 | |
). | |
% Zadatak 4 % | |
dan(pon, 1). | |
dan(uto, 2). | |
dan(sri, 3). | |
dan(cet, 4). | |
dan(pet, 5). | |
dan(sub, 6). | |
dan(ned, 7). | |
dan_u_tjednu(Dan, Broj):- | |
dan(Dan, Broj). | |
% Zadatak 5 % | |
podlista(L1, L2):- | |
podlista(L1, L2, L1). | |
podlista([], L, _). | |
podlista([G | R], [G1 | R1], Tmp):- | |
podlista(Tmp, R1, Tmp). | |
podlista([G | R], [G | R1], Tmp):- | |
podlista(R, R1, Tmp). | |
% Or this solution -> don't work for every case % | |
podlista_second([], _). | |
podlista_second([G | R], [G | R1]):- | |
podlista(R, R1). | |
% Zadatak 6 % | |
pomnozi([X], X). | |
pomnozi([G | R], Res):- | |
integer(G), | |
pomnozi(R, Res1), | |
Res is G * Res1. | |
:- op(600, xfx, voli). | |
% Zadatak 7 % | |
ivek voli barica. | |
barica voli joza. | |
barica voli ivek. | |
joza voli stefica. | |
stefica voli joza. | |
ljubavni_par( X, Y ) :- | |
X voli Y, | |
Y voli X. | |
parovi(L):- | |
setof(par(X, Y), X voli Y, L). | |
% Zadatak 8 % | |
% Predicate for substraction % | |
oduzmi([A, B, C, D, E], [F, G, H, I, J], [R1, R2, R3, R4, R5]):- | |
R1 is A - F, | |
R2 is B - G, | |
R3 is C - H, | |
R4 is D - I, | |
R5 is E - J. | |
% Operator for substraction % | |
:- op(600, xfx, vminus). | |
:- op(700, xfx, vjednako). | |
[A, B, C, D, E] vminus [F, G, H, I, J] vjednako [R1, R2, R3, R4, R5]:- | |
R1 is A - F, | |
R2 is B - G, | |
R3 is C - H, | |
R4 is D - I, | |
R5 is E - J. | |
% Zadatak 9 % | |
:- op(600, xfx, mputa). | |
:- op(700, xfx, mjednako). | |
[[X1, X2], [X3, X4]] mputa [[Y1, Y2], [Y3, Y4]] mjednako[[R1, R2], [R3, R4]]:- | |
R1 is (X1 * Y1) + (X2 * Y3), | |
R2 is (X1 * Y2) + (X2 * Y4), | |
R3 is (X3 * Y1) + (X4 * Y3), | |
R4 is (X3 * Y2) + (X4 * Y4). | |
mputa([[X1, Y1], [X2, Y2]], [[X3, Y3], [X4, Y4]], [[R1, R2], [R3, R4]]):- | |
R1 is (X1 * Y1) + (X2 * Y3), | |
R2 is (X1 * Y2) + (X2 * Y4), | |
R3 is (X3 * Y1) + (X4 * Y3), | |
R4 is (X3 * Y2) + (X4 * Y4). | |
% Zadatak 10 % | |
:- op(600, xfx, +++). | |
:- op(700, xfx, ===). | |
[] +++ L2 === L2. | |
[G | R] +++ L2 === [G | Tmp]:- | |
R +++ L2 === Tmp. | |
% Zadatak 11 % | |
rodjen( ivek, 1986 ). | |
rodjen( joza, 1989 ). | |
rodjen( bara, 1990 ). | |
rodjen( stef, 1977 ). | |
god_rac(C, G, R):- | |
rodjen(C, T), | |
R is G - T. | |
starosti(L, G):- | |
bagof((star(X, Y)), god_rac(X, G, Y), L). | |
% Zadatak 12 % | |
code([], []). | |
code([G | R], Res):- | |
code(R, Res1), | |
Crypt is G + 1, | |
%char_code(Letter, Crypt), | |
Res = [Crypt | Res1]. | |
kodiraj(I, Res):- | |
I = L, | |
code(L, Tmp), | |
%concat_atom(Tmp, Res). | |
codes_to_term(Tmp, Res). | |
% ----------------------------------------------------------------- % | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment