You are on page 1of 2

AI CONSTRUCTION Week 2 Tutorial Questions

Use Prolog language to solve the following questions:

Question 1.
1.1. Get the third element of a list L. 1.2. Insert an element into the last position of a list L. 1.3. Get the nth element of a list L. 1.4. Append two lists L1 and L2 into L3. 1.5. Reverse a list L.

Question 2.
Write postfix(L1,L) predicate which check if the list L1 is postfix of list L. Ex : postfix([4,5],[3,a,4,5]) => yes postfix([3,a],[3,4,5]) = > no

Question 3.
Write separate(L, L1, L2) predicate where L is a list of integers, the output L1 is a list of odd elements in L and the output L2 is a list of even elements in L. Ex: separate([2, 4, 3, 10, 14, 1, 7], L1, L2) => L1 = [2, 4, 10, 14] L2 = [3, 1, 7]

Question 4.
Write check predicate to check that a list of integers satisfies: the element at the position i is greater than the product of two elements at two positions i 1 and i 2 (i>= 3, i is odd) the element at the position i is smaller than the product of two elements at two

positions i 1 and i 2 (i>= 4, i is even) Ex: check([2, 1, 4, 2, 9, 3]) => yes

Question 5.
Write change predicate to convert a list of integers to a list eliminated all duplicated elements. Ex: change([2, 3, 7, 2, 4, 5, 3], X) => X = [2, 3, 7, 4, 5]

Question 6.
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

You might also like