You are on page 1of 3

TRNG I HC BCH KHOA TP.

HCM Khoa Khoa hc & K thut My tnh

ARTIFICIAL INTELLIGENCE
Tutorial 1 Solutions PROLOG
Use Prolog language to solve the following questions: Question 1. 1.1. Get the second element of a list L. 1.2. Reverse a list L. 1.3. Insert an element into the last position of a list L. 1.4. Append two lists L1 and L2 into L3. Solution: 1.1. getSecond([_, H | _], H). 1.2. reverseL([], []). reverseL([H | T], L) :- reverseL(T, T1), append(T1, [H], L). 1.3. insertLast(H, L, R) :- append(L, [H], R). 1.4. append2l([], X, X). append2l([H | T], X, [H | T1]) :- append2l(T, X, T1). Question 2. 2.1. Write count1(L, R) predicate where L is a list of integers and the output R is the number of odd numbers in L. Ex: count1([1, 2, -1, 3, 2], R) => R = 3 2.2. Write count2(LL, R) predicate where LL is 2-dimentional list of integers and the output R is the number of odd numbers in LL. Ex: count2([[1, 2, -1, 3, 2], [3, 15], [2]], R) => R = 5 2.3. Write count3(LL, R) predicate where LL is 2-dimentional list of integers and the output R is the number of elements in LL that the sum of the numbers in that element ends with 2. Ex: count3([[1, 2, 3, 6], [2, 3, 4], [3, 4], [4, 5, 6, 7]], R) => R = 2 Solution: 2.1. count1([], 0). count1([H | T], R) :- H mod 2 =\= 0, !, count1(T, R1), R is R1 + 1. count1([_ | T], R) :- count1(T, R).

TRNG I HC BCH KHOA TP.HCM Khoa Khoa hc & K thut My tnh 2.2. count2([], 0). count2([H | T], R) :- count1(H, R1), count2(T, R2), R is R1 + R2. 2.3. sum([], 0). sum([H | T], X) :- sum(T, X1), X is H + X1. count3([], 0). count3([H | T], R) :- sum(H, N), N mod 10 =:= 2, !, count3(T, R1), R is R1 + 1. count3([_ | T], R) :- count3(T, R). Question 3. Write separate(L, N, L1, L2) predicate where L is a list of integers, the output L1 is a list of the first N elements in L and the output L2 is a list of the elements left in L. Ex: separate([2, 4, 8, 10, 14, 1, 7], 3, L1, L2) => L1 = [2, 4, 8] L2 = [10, 14, 1, 7] Solution: separate(X, 0, [], X). separate([H | T], N, [H | T1], T2) :- N1 is N - 1, separate(T, N1, T1, T2). Question 4. Write calculate predicate to calculate the value of a sequence of numbers defined as follows: m, n >= 0 a(0, n) = n + 1 a(m, 0) = a(m 1, 1) if m 0 a(m, n) = a(m 1, a(m, n - 1)) if m 0 and n 0 Ex: calculate(1, 1, X) => X = 3 Solution: calculate(0, N, X) :- N >= 0, X is N + 1, !. calculate(M, 0, X) :- M > 0, !, M1 is M - 1, calculate(M1, 1, X). calculate(M, N, X) :- M > 0, N > 0, M1 is M - 1, N1 is N - 1, calculate(M, N1, X1), calculate(M1, X1, X). Question 5. Write check predicate to check that a list of integers satisfies: the element at the position i is greater than the sum of two elements at two positions i 1 and i 2 (i >= 3, i is odd)

TRNG I HC BCH KHOA TP.HCM Khoa Khoa hc & K thut My tnh the element at the position i is smaller than the sum of two elements at two positions i 1 and i 2 (i >= 4, i is even) Ex: check([2, 1, 4, 2, 9, 8]) => yes Solution: check([]). check([_]). check([_, _]). check([E1, E2, E3]) :- E3 > E1 + E2. check([H1, H2, H3, H4 | T]) :- H3 > H1 + H2, H4 < H2 + H3, check([H3, H4 | T]). Question 6. Write trans predicate to transpose a matrix A. Ex: trans([[1, 2, 3], [4, 5, 6], [7, 8, 9]], X) => X = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] Solution: trans2([], _, []). trans2([H1 | T1], N, [H2 | T2]) :- nth(N, H1, H2), trans2(T1, N, T2). trans1(_, N, B, []) :- N > B, !. trans1(L, N, B, [H | T]) :- trans2(L, N, H), N1 is N + 1, trans1(L, N1, B, T). trans([H | T], Result) :- length(H, B), trans1([H | T], 1, B, Result). Question 7. Write pack predicate to group the same consecutive elements of a list into sublists. Ex: pack([a, a, a, a, b, c, c, a, a, d, e, e, e, e], X) => X = [[a, a, a, a], [b], [c, c], [a, a], [d], [e, e, e, e]] Solution: pack([], []). pack([H, H | T] , [[H | T1] | T2]) :- pack([H | T], [T1 | T2]), !. pack([H | T], [[H] | T1]) :- pack(T, T1). -- End --

You might also like