prolog, user choice coffee menu

2019-08-27 09:28发布

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).

标签: list menu prolog
1条回答
淡お忘
2楼-- · 2019-08-27 09:51

Here is something simple.

You first need a table of options and prices, but in Prolog these can be done simply as facts.

price(moca,2.0).
price(hotChocolate,1.5).
price(latte,2.5).
price(cappuccino,3.0).

price(cold,0.1).
price(hot,0.5).

price(short,1.0).
price(tall,1.5).
price(grande,2.0).
price(venti,2.5).
price(trenta,3.0).

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.

coffeeOrder(Options,Price)

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

% Do something when the list is empty.
coffeeOptions([], ... ). 

and a predicate to handle processing the list recursively

% Do something when the list is not empty.
coffeeOptions([H|T],PriceIn,PriceOut) :-
    % do something with the head, H
    coffeeOptions(T,NewPrice,PriceOut).

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:

coffeeOrder(Options,Price) :-
    coffeeOptions(Options,0.0,Price).  % This sets the initial price to 0.0.

% Do something when the list is empty.
coffeeOptions([],Price,Price).

% Do something when the list is not empty.
coffeeOptions([Option|T],Price0,Price) :-
    price(Option,Cost),
    Price1 is Price0 + Cost,
    coffeeOptions(T,Price1,Price).

And a quick test.

?- coffeeOrder([moca,hot,grande],Price).
Price = 4.5.

All of the code as one snippet.

coffeeOrder(Options,Price) :-
    coffeeOptions(Options,0.0,Price).

coffeeOptions([],Price,Price).

coffeeOptions([Option|T],Price0,Price) :-
    price(Option,Cost),
    Price1 is Price0 + Cost,
    coffeeOptions(T,Price1,Price).

price(moca,2.0).
price(hotChocolate,1.5).
price(latte,2.5).
price(cappuccino,3.0).

price(cold,0.1).
price(hot,0.5).

price(short,1.0).
price(tall,1.5).
price(grande,2.0).
price(venti,2.5).
price(trenta,3.0).
查看更多
登录 后发表回答