天气预报

发布于 2024-12-06 14:54:33 字数 587 浏览 0 评论 0原文

彼得根据天气预报决定周末做什么。

这是可用的信息: 周六天气晴朗,周日天气晴朗。 每当阳光明媚的时候,彼得就去海滩。 每当下雨天,他就呆在家里。 当天气不确定时,这取决于日期:周六他去看电影,周日他和家人一起散步。
在 Prolog 中表示前面的句子。
制定可以回答以下问题的查询:
彼得下周六会做什么?
彼得下周日会留在家里吗?
这是我编写的代码,但不起作用:

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

现在我不知道应该做什么查询来回答问题....我想我可能会做这样的事情:

:-out(saturday,_,X).

但它不起作用.. 。 如果有人能帮助我那就太好了。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‖放下 2024-12-13 14:54:33

它不起作用的主要原因是你无法统一你的事实。如果您将 prolog 程序视为查询而不是程序,那么构建 prolog 程序会更容易。在您的代码中,您已经匹配了 suny 和 sunny。如果您修复该拼写错误,您会得到以下结果:

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

这可能仍然不是您想要的,因为它仍然匹配太多内容。试试这个:

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.

在这段代码中,我们将天气事实指定为一个过程,并在花药中指定彼得的偏好(主要是为了清楚起见)。然后我们可以查询事实并得到(更有可能)您想要的结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文