Programming paradigms-prolog predicate


Question 1: Write a Prolog predicate has_duplicates (L) which is true if list L contains duplicated elements (i.e., at least 2 copies of an element). For example:

?- has_duplicates([a,e,b,d,s,e]).
Yes
?- has_duplicates([a,b,d,s,e]).
No
?- has_duplicates([]).
No

Question 2: Write a Prolog predicate remove_nth (N, L1, L2) which is true if list L2 is just list L1 with its Nth element removed. If L1 does not have an Nth element then the predicate must fail. You can suppose that N is strictly greater than 0. For example:

?- remove_nth(4,[a,e,e,d,s,e],L).
L = [a,e,e,s,e] ;
No
?- remove_nth(6,[a,b],L).
No

Question 3: Define a Prolog predicate flatten (List, FlattenedList) which asserts List is any nested list of atoms and FlattenedList is the same list with the nesting removed. The atom [] must as well be removed. Your predicate must only produce one answer. You might use the built-in predicates not, ! and append. Don’t use a helper predicate.

?- flatten([a, [[b,c],d], [[e]], [f]], X).
X = [a,b,c,d,e,f] ;
no
?- flatten([a,[[]], [[c,d],e]], X).
X = [a,c,d,e] ;
no

Question 4: Implement a two-dimensional table in Prolog. Your program will include:

a) An insert_entry predicate that takes a table, row, column and an entry and inserts the entry at the given position creating a new table.

b) A search_entry predicate that takes a table, row and column and unifies its fourth argument with entry at the indicated place of the table.

c) Write code which creates a table and populates it with at least 2 rows and 2 columns.

Question 5: We want to make a structure which will represent a deck of cards (not essentially full). Each card has a character (between  ‘2’  to  ‘9’ and  ‘T’,  ‘J’,  ‘Q’,  ‘K’,  ‘A’) and a suit (diamonds, hearts, clubs and spades):

a) Select a representation for each card and as well for the deck of cards (it can be empty or non-empty)

b) Define an add_card predicate which takes a card and a deck and adds the card if it is not already there (that is, no duplicates) making a new deck of cards. If the card is not of a valid suit or number this predicate will simply do nothing.

c) Define a sort_deck predicate which sorts a deck of cards putting diamonds first, hearts second, clubs third and spades at the end (each suit in ascending order itself).

d) Comprise a test case in your program which creates a deck of at least 5 cards and sorts it.

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Programming paradigms-prolog predicate
Reference No:- TGS0419

Expected delivery within 24 Hours