Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

CSC305 – PROGRAMMING PARADIGMS

PROLOG: LAB 1

NAME : ZIYAD ZAHIDIN BIN YUSRI AMRI


STUDENT NO. : 2021894702
GROUP : RCS1105A

OBJECTIVE 1: WRITTING FACTS

1.1 CONVERTING ENGLISH STATEMENTS INTO FACTS IN PROLOG

Example 1 - Given the following statements:

Mary likes to play tennis.


Boris likes to play football.
Erica likes to swim.

Write facts statements to represent each of the above statement.

likes(mary,tennis).
likes(boris,football).
likes(boris,tennis).
likes(erica,swim).

Color code Descriptions


 Are known as relations.
 It must be written first.
 Must be in lower case letter.
 Are known as objects.
 Objects must start with a lower-case letter.
 Must be enclosed in parentheses.
 If there is more than one object in the parentheses, each object is separated by comma.
 Every fact must be terminated with a period character.

OBJECTIVE 2: WRITTING QUERY

2.1 SIMPLE QUERY

Example 1:
?- likes(mary,tennis).

 Every query must start with a “?-“ and ends with a period character “.”
 Simple query can only give “yes” or “no” as a response

What will be the response for the given query above?

Example 2 – What will be the response for the given query?


i) ?- likes(erica,football).
ii) ?- likes(boris,fishing).

2.2 VARIABLE QUERY

Example 1:

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


CSC305 – PROGRAMMING PARADIGMS
PROLOG: LAB 1

?- likes(mary,What).

Color code Description


 This is a variable
 A variable starts with an upper-case letter
 Every variable query will respond with a value.

What will be the response for the given query above?

Example 2:

?- likes(mary,_).

Color code Description


 This is an anonymous variable
 This type of query can only give “yes” or “no” as a response
 An anonymous variable is also used when the rules require a variable, but that variable will never be
subsequently used

What will be the response for the given query above?

2.3 QUERIES WITH AND OPERATOR


AND
Example 1: operator

?- likes(boris,tennis),likes(mary,tennis).

What will be the output?

Example 2: What will be the output?

?- likes(boris,A),likes(mary,A).

Try these..

Question 1

Given the following statement:

Miss Azlina teaches CSC305 and CSC258. Mr. Ali teaches CSC318 and CSC258.

Which of the following is the CORRECT prolog fact related to the above statement?

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


CSC305 – PROGRAMMING PARADIGMS
PROLOG: LAB 1

A. teaches(azlina,csc305).
teaches(azlina,csc258).
teaches(ali,csc318).
teaches(ali,csc258).
B. teaches(azlina,csc305,csc258).
teaches(ali,csc318,csc258).
C. teachers(azlina,ali).
subjects(csc258,csc305, csc318).
D. teacher(azlina) :- subjects(csc305,csc258).
teacher(ali) :- subjects(csc318,csc258).

Question 2

Given the following facts in Prolog:

speaks(boris,russian).
speaks(john,english).
speaks(mary,russian).
speaks(mary,english).

Write queries and outputs for each of the following questions:


a) Display the names who can speak Russian.
b) Display the names who can speak English.
c) Display the languages that Mary can speak.

a) Query : ?- speaks(Name, russian).

Output : Name = boris ;


Name = mary ;

b) Query : ?- speaks(Name, english).

Output : Name = john ;


Name = mary ;

c) Query : ?- speaks(mary, Language).

Output : Language = russian ;


Language = english ;

Question 3

a) Based on the given table, convert the English statements into facts in Prolog:

Food/beverage Price(RM)
Beriyani Rice 18.00
Kebab 10.00
Nan Set 25.00
Iced Tea 2.50
Iced Milo 3.00

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


CSC305 – PROGRAMMING PARADIGMS
PROLOG: LAB 1

b) Write queries to find out the price for:


i. A set of nan
ii. A glass of iced tea
iii. Kebab

c) Write a query to find out which food/beverage is RM18.00? State the output as well.

d) Write a query that will list down all the foods/beverages with prices below than RM15.00. State the output as well.

a) food_price('Beriyani Rice', 18.00).


food_price('Kebab', 10.00).
food_price('Nan Set', 25.00).
food_price('Iced Tea', 2.50).
food_price('Iced Milo', 3.00).

b) ?- food_price('Nan Set', Price).


Price = 25.00.
?- food_price('Iced Tea', Price).
Price = 2.50.
?- food_price('Kebab', Price).
Price = 10.00.

c) ?- food_price(Food, 18.00).
Food = 'Beriyani Rice'.

d) ?- food_price(Food, Price), Price < 15.00.


Food = 'Kebab',
Price = 10.00 ;
Food = 'Iced Tea',
Price = 2.50 ;
Food = 'Iced Milo',
Price = 3.00.

Question 4

Given the following fact statements on a theme park entrance fee:

ticketPrice(packageOne,55).
ticketPrice(packageTwo,75).
ticketPrice(packageThree,110).

a) What will be the respond for the given query below:


i. ?- ticketPrice(packageOne,Y).
ii. ?- ticketPrice(X,110).
iii. ?- ticketPrice(_,85).

b) Write a query that will display all the packages with fee less than RM80.

c) Based on your query in (b), what will be the output?

d) Write a query that will display the name of the package which has price more than RM70 but less than RM100.

a) i. Y = 55.

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


CSC305 – PROGRAMMING PARADIGMS
PROLOG: LAB 1

ii. Output:
```
X = packageThree.
```
iii. No output will be generated because there is no package with a price of 85 in the given facts.

b) ?- ticketPrice(Package, Price), Price < 80.

c) Package = packageOne,
Price = 55 ;
Package = packageTwo,
Price = 75.

d) ?- ticketPrice(Package, Price), Price > 70, Price < 100.


Package = packageTwo.

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU

You might also like