Prolog - 描述事实和规则 [英] Prolog - Describe facts and rules

查看:65
本文介绍了Prolog - 描述事实和规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在序言中描述以下事实和规则:

I want to describe in prolog the following facts and rules:

  1. Nick 正在使用 Java 编程.
  2. Nick 正在使用 Python 编程
  3. Nick 是任何使用 Java 和 Python 编程的朋友
  4. Jim 会使用 Nick 使用的所有语言进行编程.

我找到了第 1、2 和 3 项的解决方案,但没有找到第 4 项的解决方案,尽管我真的很感激一个完整的解决方案.

I found the solution for 1, 2 and 3 but not for the 4th, even though i would really appreciate a full solution.

我的解决方案:

male(Nick).

male(Jim).

programming(Nick, java).

programming(Nick, python).

friends(X,Y):-
    programming(X,java),
    programming(X,python),
    programming(Y,java),
    programming(Y,python),

推荐答案

您的解决方案中存在一些错误:

There are a few errors in your solution:

  • 大写字母开头的常量(如Nick)不是常量;而是一个变量.因此该行:

  • A constant (like Nick) starting with a capital letter is no constant; but a variable. Thus the line:

male(Nick).

说每个人都是male/1;你应该更正它:

says that everyone is a male/1; you should correct it to:

male(nick).
male(jim).

(顺便说一下,programming/2 也是如此).此外,这似乎不是作业的一部分 (?).

(the same for programming/2 by the way). furthermore this doesn't seem to be part of the assignment (?).

friends/2 谓词以逗号 (,) 结尾,这意味着 Prolog 解析器需要更多输入,并将下一个事实视为当前条款;你应该用点 (.);

The friends/2 predicate ends with a comma (,) meaning that the Prolog parser expects more input, and will see the next fact as part of the current clause; you should end clauses with the dot (.);

friends/2 谓词在语义上是不正确的,因为这个问题只对 Nick 作出陈述:因此你不能使用 X 作为人,你应该像这样专门化它:

The friends/2 predicate is not semantically correct, since the question only makes statements about Nick: you thus can't use X as the person, you should specialize it like:

friends(nick,Y):-
    programming(Y,java),
    programming(Y,python).

你的 friends/2 版本说:如果 X 和 Y 都可以用 Java 和 Python 编程,那么 X 就是 Y 的朋友";尽管这导致 Nick 是使用 Java 和 Python 编程的每个人的朋友,但您的陈述比应允许的范围更广:例如,我们不知道 Jim 是否根据这些规则决定谁与他是朋友.例如,Jim 有可能(尽管可能不太可能)想从他的朋友那里学习东西,例如,他只与至少知道一种他不掌握的编程语言的人成为朋友.

Your version of friends/2 said: "A person X is a friend of a person Y, if both X and Y can program in Java and Python"; although this results in the fact that Nick is a friend of everyone that programs in Java and Python, your statements is broader than what should be allowed: we don't know if Jim for instance decides who is friends with him based on these rules. It is for instance possible (although perhaps not likely) that Jim wants to learn things from his friends, and for instance is only friends with people that know at least one programming language he doesn't master.

最后一个问题可以写成:

The last question can be written as:

programming(jim,X) :-
    programming(nick,X).

对这句话的几乎机械翻译是:Jim 在 X 中编程 if nick 在 X 中编程";(请注意这不是if-and-only-if);因此您仍然可以添加 Jim 可以使用的其他语言.

an almost mechanical translation of the statement is: "Jim is programming in X if nick is programming in X"; (mind this is no if-and-only-if); so you can still add additional languages Jim can work with.

这篇关于Prolog - 描述事实和规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆