天气预报
彼得根据天气预报决定周末做什么。
这是可用的信息: 周六天气晴朗,周日天气晴朗。 每当阳光明媚的时候,彼得就去海滩。 每当下雨天,他就呆在家里。 当天气不确定时,这取决于日期:周六他去看电影,周日他和家人一起散步。
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不起作用的主要原因是你无法统一你的事实。如果您将 prolog 程序视为查询而不是程序,那么构建 prolog 程序会更容易。在您的代码中,您已经匹配了 suny 和 sunny。如果您修复该拼写错误,您会得到以下结果:
这可能仍然不是您想要的,因为它仍然匹配太多内容。试试这个:
在这段代码中,我们将天气事实指定为一个过程,并在花药中指定彼得的偏好(主要是为了清楚起见)。然后我们可以查询事实并得到(更有可能)您想要的结果。
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:
Which is still probably not what you want because it's still matching too many things. try this instead:
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.