Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prolog can be used as a sophisticate database system in which data is stored in

ID: 3536667 • Letter: P

Question

Prolog can be used as a sophisticate database system in which data is stored in the form of structured predicates. In this problem we consider a database of smoothie stores. Each store has a name, a list of employees, and a list of smoothie that can be purchased in the store, which are encoded in a store predicate. Each smoothie is defined by a name, a list of fruits, and a price, which are encoded in a smoothie predicate. For example, here are three predicates defining three different smoothie stores:

store(best_smoothies, [alan,john,mary],
     [ smoothie(berry, [orange, blueberry, strawberry], 2),
     smoothie(tropical, [orange, banana, mango, guava], 3),
     smoothie(blue, [banana, blueberry], 3) ]).

store(all_smoothies, [keith,mary],
     [ smoothie(pinacolada, [orange, pineapple, coconut], 2),
     smoothie(green, [orange, banana, kiwi], 5),
     smoothie(purple, [orange, blueberry, strawberry], 2),
     smoothie(smooth, [orange, banana, mango],1) ]).

store(smoothies_galore, [heath,john,michelle],
     [ smoothie(combo1, [strawberry, orange, banana], 2),
       smoothie(combo2, [banana, orange], 5),
     smoothie(combo3, [orange, peach, banana], 2),
     smoothie(combo4, [guava, mango, papaya, orange],1),
     smoothie(combo5, [grapefruit, banana, pear],1) ]).

The first store has three employees and sells three different smoothies, the second store has two employees and sells four different smoothies, and the third store has three employees and sells five different smoothies. You can assume that there are no duplicates (pineapple is not listed twice in any ingredient list, mary is not listed twice in any employee list, the same smoothie specification is not listed twice in any store menu, etc.). Given a database of smoothie store facts, the questions below have you write predicates that implement queries to the database.

#1: Sells More than Four Smoothies

Write a Prolog predicate more_than_four(X) that is true if store X has four or more smoothies on its menu. For instance:

?- more_than_four(best_smoothies).
No

?- more_than_four(X).
X = all_smoothies ;
X = smoothies_galore ;
No

#2: exists

Write a Prolog predicate exists(X) that is true if there is a store that sells a smoothie named X. For instance:

?- exists(combo1).
Yes

?- exists(slimy).
No

?- exists(X).
X = berry ;
X = tropical <enter>
Yes

#3: Employee to Smoothie Ratio

Write a Prolog predicate ratio(X,R) that is true if there is a store named X, and if R is the ratio of the store's number of employees to the store's number of smoothies on the menu. For instance:

?- ratio(all_smoothies,R).
R = 0.5 ;
No

?- ratio(Store,R).
Store = best_smoothies
R = 1 ;
Store = all_smoothies
R = 0.5 ;
Store = smoothies_galore
R = 0.6 ;
No

Hint

Explanation / Answer

minmax([First|Rest], Min, Max) :- minmax(Rest, First, First, Min, Max). minmax([], Min, Max, Min, Max). minmax([Value|Ns], MinCurr, MaxCurr, Min, Max) :- .... minmax(Ns, MinNext, MaxNext, Min, Max).