开发者

Weather predictions

Peter decides what to do at weekends according to the weather predictions.

This is the available information: Saturday will be sunny, Sunday will be uncertain. whenever it is sunny, Peter goes to the beach. Whenever it's rainy he stays at home. When the weather is uncertain it depends on the day: Saturdays he goes to the cinema, Sundays 开发者_如何学Gohe goes for a walk with his family.

Represent in Prolog the previous sentences.

Formulate queries which allow to answer the following questions:

What will Peter do next Saturday?

Will Peter stay at home next Sunday?

Here's the code I've made and that doesn't work:

out(saturday,suny,_). 
out(sunday,uncertain,_).

out(saturday,sunny,beach).
out(sunday, sunny, beach).
out(saturday,rainny,home).
out(sunday, rainny,home).


out(saturday,uncertain,cinema).
out(sunday,uncertain,family).

Now I don't know what queries should I make to answer the questions.... I think I might do something like this:

:-out(saturday,_,X).

But it doesn't work... If anyone can help me that would be great.


The main reason it doesn't work is that you can't unify your facts. It's easier to construct prolog programs if you think about them in terms of being a query rather than a program. In your code, you have out matching suny AND sunny. if you fix that spelling error, you get this:

?- out(saturday,_,X).
true ;
X = beach ;
X = home ;
X = cinema ;
true.

Which is still probably not what you want because it's still matching too many things. try this instead:

weather(saturday, sunny).
weather(sunday, uncertain).

prefers(peter, if_weather(uncertain), onday(sunday), walk_with_family).
prefers(peter, if_weather(sunny), onday(_), go_to_beach).
prefers(peter, if_weather(uncertain), onday(saturday), go_to_cinema).
prefers(peter, if_weather(rainy), onday(_), stay_home).

peter_will_do_next(Day,X) :- prefers(peter, if_weather(Y), onday(Day), X),    weather(Day,Y).
peter_will_stay_home_on(Day) :- peter_will_do_next(Day,Y), Y == stay_home.

?- peter_will_do_next(saturday,What).
What = go_to_beach .

?- peter_will_stay_home_on(sunday).
false.

In this code, we specify the facts of the weather as one procedure and the preferences of peter in anther (mostly for clarity). We can then query the facts and get a result that is (more likely) what you had in mind.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜