现在的大学生,这个学期必须学习Prolog。在我们实际进入任何任务之前想要捣乱并继续前进并学习它我会失去大声笑。
我正在为一个专门用于电影的节目创建一个项目
我的movie.pl文件包含以下内容:
%acted_in(person,movie).
acted_in(brad_pitt,babel).
acted_in(cate_blanchette,babel).
acted_in(sharlto_copley,district_9).
acted_in(david_james,district_9).
%directed(person,movie).
directed(alejandro_inarritu,babel).
directed(neill_blomkamp,district_9).
%released(movie,year).
released(babel,2006).
released(district_9,2009).
这些只是我在那里的一些。现在我的老师确实已经有了东西,因为他建议我们继续尝试我们自己的程序。他继续向我们提供了一个makefile,它可以用来改变EXE =电影所需的一切。
我遗失的部分是在main.pl.假设我有多个不同的问题,我将如何添加它们?我只是在之前的print_query_false下添加一个带有新问题的writeln吗?
%main.pl
:- [movie].
print_query_true(Q) :-
forall(Q, writeln(true:Q)).
print_query_false(Q) :-
forall(\+ Q, writeln(false:Q)).
main :-
nl
writeln( "1. Did Leonardo DiCaprio act in Babel?" ),
print_query_true(acted_in(leonardo_dicaprio,babel)),
print_query_false(acted_in(leonardo_dicaprio,babel)),
nl,
halt.
我遇到的最后一个问题是,当我尝试运行swipl时,它会通过并打开程序。但是当我输入[电影]。它只是说是真的,并没有表明是编译或任何类型。
答案 0 :(得分:0)
我遗失的部分是在main.pl.假设我有多个不同的问题,我将如何添加它们?我只是在之前的print_query_false下添加一个带有新问题的writeln吗?
是的,例如:
main :-
nl,
format( "1. Did Leonardo DiCaprio act in Babel?" ),
nl,
print_query_true(acted_in(leonardo_dicaprio,babel)),
print_query_false(acted_in(leonardo_dicaprio,babel)),
nl,
format( "2. Was babel released in 2006?" ),
nl,
print_query_true(released(babel, 2006)),
print_query_false(released(babel, 2006)),
nl,
format( "3. Did anyone act in both Click and The Aviator?" ),
nl,
print_query_true((acted_in(X, click), acted_in(X, the_aviator))),
print_query_false((acted_in(X, click), acted_in(X, the_aviator))),
nl,
format("4. Did sharlto_copley and david_james both act in district_9?"),
nl,
print_query_true((acted_in(sharlto_copley, district_9), acted_in(david_james, district_9))),
print_query_false((acted_in(sharlto_copley, district_9), acted_in(david_james, district_9))),
nl,
format("5. Was there any two movies released in 2006 and 2009?"),
nl,
print_query_true((released(X, 2006), released(Y, 2009))),
print_query_false((released(X, 2006), released(Y, 2009))),
nl,
format("6. What actors acted in babel or district 9?"),
nl,
print_query_true((acted_in(X, babel) ; acted_in(X, district_9))),
print_query_false((acted_in(X, babel) ; acted_in(X, district_9))),
nl,
format("6. What actors played in babel but not in district 9?"),
nl,
print_query_true((acted_in(X, babel), \+ acted_in(X, district_9))),
print_query_false((acted_in(X, babel), \+ acted_in(X, district_9))),
nl,
halt.
我将writeln
更改为format
以在输出中获取正确的格式化字符串。试运行:
?- [main].
% movie compiled 0.00 sec, 9 clauses
% main compiled 0.00 sec, 15 clauses
true.
?- main.
1. Did Leonardo DiCaprio act in Babel?
false:acted_in(leonardo_dicaprio,babel)
2. Was babel released in 2006?
true:released(babel,2006)
3. Did anyone act in both Click and The Aviator?
false: (acted_in(_G1551,click),acted_in(_G1551,the_aviator))
4. Did sharlto_copley and david_james both act in district_9?
true: (acted_in(sharlto_copley,district_9),acted_in(david_james,district_9))
5. Was there any two movies released in 2006 and 2009?
true: (released(babel,2006),released(district_9,2009))
6. What actors acted in babel or district 9?
true: (acted_in(brad_pitt,babel);acted_in(brad_pitt,district_9))
true: (acted_in(cate_blanchette,babel);acted_in(cate_blanchette,district_9))
true: (acted_in(sharlto_copley,babel);acted_in(sharlto_copley,district_9))
true: (acted_in(david_james,babel);acted_in(david_james,district_9))
6. What actors played in babel but not in district 9?
true: (acted_in(brad_pitt,babel),\+acted_in(brad_pitt,district_9))
true: (acted_in(cate_blanchette,babel),\+acted_in(cate_blanchette,district_9))
SWI-Prolog有一个很棒的文档,所以我建议你检查用于完全理解它们的谓词:
我遇到的最后一个问题是,当我尝试运行swipl时,它会通过并打开程序。但是当我输入[电影]。它只是说是真的,并没有表明是编译或任何类型。
确保您所在的目录中有源文件。
SWI-Prolog v6.6.4输出:
?- [movie].
% movie compiled 0.00 sec, 9 clauses
true.