0% found this document useful (0 votes)
82 views4 pages

9.0 Prolog: Q1 Find The Total Cost of Items

The document describes two Prolog queries: 1. To find the total cost of items in a list by recursively summing the costs of individual items stored in facts. 2. To count the number of items in a list by recursively iterating through the list and incrementing a counter variable. Both queries use recursion and pattern matching on lists to iterate through items, access item costs from facts, and accumulate results.

Uploaded by

Kumar Adityaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views4 pages

9.0 Prolog: Q1 Find The Total Cost of Items

The document describes two Prolog queries: 1. To find the total cost of items in a list by recursively summing the costs of individual items stored in facts. 2. To count the number of items in a list by recursively iterating through the list and incrementing a counter variable. Both queries use recursion and pattern matching on lists to iterate through items, access item costs from facts, and accumulate results.

Uploaded by

Kumar Adityaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

9.

0 Prolog
Q1
facts

Find the total cost of items


costofitem(cornflakes, 230).
costofitem(cocacola, 210).
costofitem(chocolate, 250).

Rule

costofitem(crisps, 190).
totalcost([], 0).
% trivial branch
totalcost([Item|Rest], Price) :- % recursive branch
costofitem(Item, Itemprice),
totalcost(Rest, PriceOfRest),
Price is Itemprice + PriceOfRest.

Query

Q2

Count the number of items

facts

costofitem(cornflakes, 230).
costofitem(cocacola, 210).
costofitem(chocolate, 250).

Rule

costofitem(crisps, 190).
count([],0).
count([Item|Rest],Total):-costofitem(Item,Itemprice),count(Rest,Trest),Total
Trest+1.
%count(List,TL):-length List

Query

is

Screenshots with explanation:PROLOG database:-Rules+Facts


PROLOG rules for pattern matching:1. Firstly it will check the no of element in the list. If element matches then it proceed.
2. Then it will check the data type for query and the fact if it matches then the value is true
else false
3. Matching in PROLOG is based on following factors.

Constant (in small letters):- data type matches with itself or Variable (capital
letters)
Variable(in Capital Letters):-data type matches with anything
Structure(in small letter):-data type matches itself or variable consider following:o Name of structure must be same
o No of components must be same
o Data type must be same

Q1
Rule:

Find the total cost of items


totalcost([], 0).
% trivial branch

totalcost([Item|Rest], Price) :- % recursive branch


costofitem(Item, Itemprice),
totalcost(Rest, PriceOfRest),
Facts:

Price is Itemprice + PriceOfRest.


costofitem(cornflakes, 230).

costofitem(cocacola, 210).
costofitem(chocolate, 250).
costofitem(crisps, 190).
Query
1 ?- totalcost([cornflakes,chocolate],X).
X = 480.
ExplanationSince in the above rule it is firstly matches with the facts. The totalcost here
takingthe List of itmes and the by the help of list constructor its first element and theremaining
element is separated the first element is stored into the item variable.And now this variable is
passed with fact and there is also one variable that will storethe price of the item that is pointed
by the item variable.The call to the fact matches with the above given fact if match found
successful thenthe price of that item is stored inside the Itemprice variable. And after that the
remaining part is again passed to the rule and the simila process continues till the list will not be
empty

Q2
Rule:count([],0).

Count the total Items in the List

count([Item|Rest],Total):-costofitem(Item,Itemprice),count(Rest,Trest),Total is
Trest+1.
%count(List,TL):-length List
Facts:
costofitem(cornflakes, 230).
costofitem(cocacola, 210).
costofitem(chocolate, 250).
costofitem(crisps, 190).
Query 2 ?- count([cornflakes,chocolate],X).
X = 2.

Explanation in this element in the list will iterate one by one through recursion and every time
whenever the rule is called the counter variable is incremented by 1 and finally it will
have the value that is equal to the number of items in the list

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy