I'm a beginner in Prolog course, I am trying to write code to allow the user to choose the coffee then ask hot or cold then the size of the coffee to calculate price. I was looking on the web explain in how to develop the program but I feel it's different from what I need in the example: [animal identification][1]. Can you please help me to write the coffee menu.
Here is what I have tried.
go :- hypothesize(Coffee),
write('Your order is : '),
write(Coffee),
write('and the price for your order = : ')
nl,
undo.
/* hypotheses to be tested */
hypothesize(moca) :- moca, !.
hypothesize(hotChocolate) :- hotChocolate, !.
hypothesize(latte) :- latte, !.
hypothesize(cappuccino) :- cappuccino, !.
/* rules */
moca :-
/* ask if you want hot or cold
* ask the size of the coffee*/
Is my method correct or better to create a list and then the user chooses by type the name of the coffee?
add the menu like this
menu :- repeat,
write('pleaase, Choose the Coffe to order:'),nl,
write('1. Moca'),nl,
write('2. Latte'),nl,
write('3. Hot Choclate'),nl,
write('Enter your choice number please: '),nl,
read(Choice),
run_opt(Choice).
Here is something simple.
You first need a table of options and prices, but in Prolog these can be done simply as facts.
Next you need to decide on the arguments for the predicate, in this case that is easy, a list of options for the input and a price for the output.
Since there are a list of options the code needs to process a list and one of the easiest ways for a beginner is to use a recursive call. A recursive set of predicates follows the pattern of a base case
and a predicate to handle processing the list recursively
When generating a value, in this case the final price, and using a recursive call, often a helper predicate is needed to set up an initial value, in this case the initial cost which is 0.0.
So the predicates are:
And a quick test.
All of the code as one snippet.