Using prolog as the programming language for logic


1. Using Prolog as the programming language for LOGIC, construct the relevant command to do the following tasks:-

a. Prolog Lists - A list is either empty or it is composed of a first element (head) and a tail, which is a list itself. In Prolog we represent the empty list by the atom [] and a non-empty list by a term [H|T] where H denotes the head and T denotes the tail. 
(NOTE:- Given list MySenarai= [a,b,c,d,e,f, g, h] for the tasks listed below)

i. Find the last element of a list.
?-Output:- 

ii. Find the K'th element of a list. (K=5)
?-Output:- 

iii. Eliminate consecutive duplicates of list elements.
?-Output:- 

iv. Find the number of elements of a list.
?-Output:- 

v. Reverse a list.
?-Output:- 

vi. Find out whether a list is a palindrome. 
?-Output:- 
vii. Duplicate the elements of a list. 
?-Output:- 

viii. Drop every N'th element from a list. (N=3)
?-Output:- 

ix. Insert an element at a given position into a list. (element = zzz)
?-Output:- 

x. Extract a slice from a list. (I = 2; k = 5)
Given two indices, I and K, the slice is the list containing the elements between the I'th and K'th element of the original list (both limits included). Start counting the elements with 1
?-Output:- 

b. Problem Case: - 'Something is fun if it's a red car or a blue bike or it is ice cream'. 

These three options are represented in Prolog by three clauses (rules or facts) of the predicate fun. Prolog will start from the first clause of fun and try that. If that does not succeed, it will try the next clause. It only fail when it runs out of rules or facts to try. Given is an example of the first rule.

fun(x) :- /* an item is fun if */
red(X), /* the item is red */
car(X). /* and it is a car */

Here are other additional fun facts. VW_Beatle is a red car , Ford Escort is a blue car. Harley_Davidson is a blue bike. Yamaha is a red bike. 

Construct the rest of the necessary clauses and commands to see if: - i) Harley_Davidson is fun and ii) Identify items which are fun. 
Explain how Prolog executes the command (query) and the goal matching process.

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Using prolog as the programming language for logic
Reference No:- TGS0114332

Expected delivery within 24 Hours