level 1
趒跃丨想唸
楼主
今年大三学习人工智能,关于归结原理的实验想用prolog实现。(其实是宿舍里面一个同学告诉我的,指导书上也推荐用这个)马虎看了一下教程关于这方面的,又看到丁兔大佬另一个类似问题的例子,自己也试着编了一下。但是语法不熟悉,出错自己也找不到,关于辨别性别方面感觉自己写的也是错的,希望大佬们能来指导一下。
问题描述:
四对夫妇中,王结婚时,周送了礼;周和钱是同一排球队的队员;李的爱人是陈的爱人的表哥;陈夫妇与邻居吵架时,徐、周、吴的爱人都去助战;李、徐、周结婚前住在同一宿舍,试用归结原理求王、周、钱、陈、李、徐、吴、孙几人谁和谁是夫妇。
我的程序:
human(wang).
hunan(zhou).
human(qian).
human(chen).
human(li).
human(xu).
human(wu).
human(sun).
%定义所有人
man(Man):-
human(Man),
\+ woman(Man).
woman(Woman):-
human(Woman),
\+ man(Woman).
%定义男女
spouse(Husband,Wife):-
man(Husband),
woman(Wife),
\+ notspouse(Husband,Wife).
%定义夫妻
notspouse(X,Y):-
notspouse(Y,X).
%spouse函数与函数参数顺序无关
spouse(H1,W1),
spouse(H2,W2),
spouse(H3,W3),
spouse(H4,W4),
man(H1),man(H2),man(H3),man(H4),
woman(W1),woman(W2),woman(W3),woman(W4),
H1 /= H2,H1 /= H3,H1/= H4,
H2 /= H3,H2 /= H4,
H3 /= H4,
W1 /= W2,W1 /= W3,W1/= W4,
W2 /= W3,W2 /= W4,
W3 /= W4.
%四队夫妻
notspouse(zhou,wang).
%王结婚时,周送了礼
notspouse(zhou,qian).
%周和钱是同一排球队的队员
notspouse(li,chen).
woman(li).
%李的爱人是陈的爱人的表哥
notspouse(chen,xu).
notspouse(chen,zhou).
notspouse(chen,wu).
notspouse(xu,zhou).
notspouse(xu,wu).
notspouse(zhou,wu).
%陈夫妇与邻居吵架时,徐、周、吴的爱人都去助战
woman(xu).
woman(zhou).
%李、徐、周结婚前住在同一宿舍(不会写直接推了)
出错情况:
Warning: c:/users/administrator/desktop/spouse.pl:3:
Clauses of human/1 are not together in the source-file
Earlier definition at c:/users/administrator/desktop/spouse.pl:1
Current predicate: hunan/1
Use :- discontiguous human/1. to suppress this message
ERROR: c:/users/administrator/desktop/spouse.pl:33:2: Syntax error: Operator expected
Warning: c:/users/administrator/desktop/spouse.pl:45:
Clauses of woman/1 are not together in the source-file
Earlier definition at c:/users/administrator/desktop/spouse.pl:14
Current predicate: notspouse/2
Use :- discontiguous woman/1. to suppress this message
Warning: c:/users/administrator/desktop/spouse.pl:47:
Clauses of notspouse/2 are not together in the source-file
Earlier definition at c:/users/administrator/desktop/spouse.pl:23
Current predicate: woman/1
Use :- discontiguous notspouse/2. to suppress this message
Warning: c:/users/administrator/desktop/spouse.pl:54:
Clauses of woman/1 are not together in the source-file
Earlier definition at c:/users/administrator/desktop/spouse.pl:14
Current predicate: notspouse/2
Use :- discontiguous woman/1. to suppress this message
Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- spouse(X,Y).
ERROR: Out of local stack
2018年04月27日 05点04分
1
问题描述:
四对夫妇中,王结婚时,周送了礼;周和钱是同一排球队的队员;李的爱人是陈的爱人的表哥;陈夫妇与邻居吵架时,徐、周、吴的爱人都去助战;李、徐、周结婚前住在同一宿舍,试用归结原理求王、周、钱、陈、李、徐、吴、孙几人谁和谁是夫妇。
我的程序:
human(wang).
hunan(zhou).
human(qian).
human(chen).
human(li).
human(xu).
human(wu).
human(sun).
%定义所有人
man(Man):-
human(Man),
\+ woman(Man).
woman(Woman):-
human(Woman),
\+ man(Woman).
%定义男女
spouse(Husband,Wife):-
man(Husband),
woman(Wife),
\+ notspouse(Husband,Wife).
%定义夫妻
notspouse(X,Y):-
notspouse(Y,X).
%spouse函数与函数参数顺序无关
spouse(H1,W1),
spouse(H2,W2),
spouse(H3,W3),
spouse(H4,W4),
man(H1),man(H2),man(H3),man(H4),
woman(W1),woman(W2),woman(W3),woman(W4),
H1 /= H2,H1 /= H3,H1/= H4,
H2 /= H3,H2 /= H4,
H3 /= H4,
W1 /= W2,W1 /= W3,W1/= W4,
W2 /= W3,W2 /= W4,
W3 /= W4.
%四队夫妻
notspouse(zhou,wang).
%王结婚时,周送了礼
notspouse(zhou,qian).
%周和钱是同一排球队的队员
notspouse(li,chen).
woman(li).
%李的爱人是陈的爱人的表哥
notspouse(chen,xu).
notspouse(chen,zhou).
notspouse(chen,wu).
notspouse(xu,zhou).
notspouse(xu,wu).
notspouse(zhou,wu).
%陈夫妇与邻居吵架时,徐、周、吴的爱人都去助战
woman(xu).
woman(zhou).
%李、徐、周结婚前住在同一宿舍(不会写直接推了)
出错情况:
Warning: c:/users/administrator/desktop/spouse.pl:3:
Clauses of human/1 are not together in the source-file
Earlier definition at c:/users/administrator/desktop/spouse.pl:1
Current predicate: hunan/1
Use :- discontiguous human/1. to suppress this message
ERROR: c:/users/administrator/desktop/spouse.pl:33:2: Syntax error: Operator expected
Warning: c:/users/administrator/desktop/spouse.pl:45:
Clauses of woman/1 are not together in the source-file
Earlier definition at c:/users/administrator/desktop/spouse.pl:14
Current predicate: notspouse/2
Use :- discontiguous woman/1. to suppress this message
Warning: c:/users/administrator/desktop/spouse.pl:47:
Clauses of notspouse/2 are not together in the source-file
Earlier definition at c:/users/administrator/desktop/spouse.pl:23
Current predicate: woman/1
Use :- discontiguous notspouse/2. to suppress this message
Warning: c:/users/administrator/desktop/spouse.pl:54:
Clauses of woman/1 are not together in the source-file
Earlier definition at c:/users/administrator/desktop/spouse.pl:14
Current predicate: notspouse/2
Use :- discontiguous woman/1. to suppress this message
Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- spouse(X,Y).
ERROR: Out of local stack