It's one of my classroom question.
I was able to create my own Prolog program with bunch of if-else but I was told that my program is not fully declarative as its one of the very foundation principle in Prolog.
Here's my code
%start with :- go.
is_spicy :-
write('Do you want spicy? (yes/no)'),
read(Preference),
( Preference == yes -> recommend("Curry"); recommend("Kurma") ).
is_fry :-
write('Do you want fry food? (yes/no)'),
read(Preference),
( Preference == yes -> recommend("StirFry"); recommend("Chicken") ).
is_chili :-
write('Do you want chili? (yes/no)'),
read(Preference),
( Preference == yes -> recommend("Sambal"); recommend("Singgang") ).
recommend(Food) :- write('We recommend you '), write(Food).
go :-
write('Which food type do you prefer? (indian, chinese, malay): '),
read(FoodType),
(
FoodType == indian -> is_spicy;
FoodType == chinese -> is_fry;
FoodType == malay -> is_chili;
writeln('Please try again')
).
Anyone has any idea on how to make it "fully declarative"?
If you address the redundancy in your code, factoring out data from logic, you end with much more declarative code.
So, encode the data structure, and provide an 'interpreter' able to ask questions and infer the consequences.
For instance
you may want to specialize the menu for y/n only choices, but it's up to you
edit
Presenting a menu and accepting choices require extra logical programming, for instance:
Example session: