Write a prolog procedure to split a given list into two


Assignment 1:

Important: The main purpose of this assignment is to develop your skills regarding the use of built-in predicates in Prolog, the control of backtracking and the way in which Prolog can be used for simple arithmetic procedures.

Please note that you need to submit screen shots (using fn+prt sc for Windows in most instances) of results for questions where a program/procedure or query is required. This will assist us to see whether you obtained the correct results and if not, try to point out where you went wrong.

Your programs should also be robust. This means that it should check whether all the input arguments for a specific procedure are legal. For example, if you know you are working with integers, an input of the constant that is not an integer is not acceptable.

Important note: It is not advisable that you search for Prolog solutions to the questions in this assignment on the internet. You may find a solution to a specific problem but that will not assist you in acquiring the necessary skills for mastering this programming paradigm.

Question 1

(a) Write a Prolog procedure to split a given list into two separate lists, one containing integers and the other one real numbers. All other items in the given list should be ignored.

(b) Briefly discuss the difference between green cuts and red cuts.

Question 2:

(a) Write a procedure filter(List,PredName,Result) that removes all the elements X in List for which PredName(X) fails, and returns the resulting list in Result. The predicate PredName/1 should be defined when calling the procedure filter.

Let test be defined as test(N):- atomic(N).

?- filter([a,b,-6,7,A,-1,0,B],test,L). L = [a,b,-6,7,-1,0],

Hint: Use the Prolog built-in predicate =..

(b) Write a procedure map(List,PredName,Result) that applies the predicate PredName(Arg,Res) to all the elements in List, and returns the result in the list Result.

Let test be defined as test(N,R):- R is N^3.

?- map([3,5,-2],test,L).

L = [27,125,-8] (6)

Question 3

Use the database you defined for question 1 of assignment 1 to determine the following using setof, bagof or findall:
(a) How many different kinds of birds are listed in the database? (4)
(b) How many birds defined in the database include insects in their diet? (5)
(c) How many different sizes of bird are defined in the database? (4)

Lists can be used to represent sets in Prolog. Write the following Prolog procedures using the Prolog built-in predicates setof, bagof or findall.

(a) The procedure intersection(A,B,L) returns the intersection L of sets A and B,i.e. the set L contains all elements that belong to A and to B. (5)

(b) The procedure complement(A,U,C) returns the complement L of set A given the universal set U (A is a subset of U). The complement of set A consists of all those elements that belong to U but not to A. (6)

Question 4

Consider the following set of Prolog clauses concerning big cats in the National Parks in South Africa.
leopard(X):-
not lion(X), not cheetah(X).
lion(simba). lion(mufasa). lion(scar).

Sometimes Prolog returns unexpected results, e.g. if the query leopard(sylvester) is entered using the information given above, Prolog retuns yes. (For those of you not familiar with Sylvester the lion: he was declared a habitual criminal after escaping twice from one of our national parks. His new home is the Addo Elephant Park in the Eastern Cape.)

(a) Explain why this happens.
(b) What assumption causes the phenomenon mentioned above?

Question 5

Construct a table listing the symbols that can be used to compare two terms X and Y and state the meaning of each. (For your own benefit, make sure you understand the meaning of each.)

Question 6
The way in which cryptarithmetic puzzles can be solved in Prolog is discussed in section 4.4 of Bratko. Use the guidelines given by Bratko to solve the following puzzle:
CROSS + ROADS = DANGER.

Question 7

The Russian Multiplication problem can be defined as follows: Say you want to multiply x with y giving z. The problem is solved using the following iterative loop:

With each iteration, x gets the value x/2 and y gets the value y*2. If x is even, the y-entry is ignored. If x is odd, y is added to a running total. The loop terminates when x = 0.

For example: Calculate z = 24 * 52.

 

x

 

y

 

Calculation of total (T)

 

Total

 

24

 

52

 

 

0

 

12

 

104

 

 

0

 

6

 

208

 

 

0

 

3

 

416

 

T = T + 416

 

416

 

1

 

832

 

T = T + 832

 

1248

 

0

 

 

 

1248

Write a Prolog program to implement the Russian Multiplication Problem.

Question 8

Assume that your database currently contains the following facts:

What are the effects of the following queries?

(a) ?- p(X),q(X,Y),r(Y,Z).
(b) ?- p(X),!,q(X,Y),r(Y,Z).
(c) ?- p(X),q(X,Y),!,r(Y,Z).
(d) ?- p(X),once(q(X,Y)),r(Y,Z).

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: Write a prolog procedure to split a given list into two
Reference No:- TGS01532391

Expected delivery within 24 Hours