[英]Prolog - turning of a list into a list of fact
我使用以下命令在Python中启动SWI-prolog:
subprocess.call("(source ~/.bash_profile && swipl -s planner.pl b a c b a table )", shell=True)
启动的脚本是一个计划器:
:- initialization (main).
:- dynamic on/2.
%facts
on(a,b).
on(b,c).
on(c,table).
r_put_on(A,B) :-
on(A,B).
r_put_on(A,B) :-
not(on(A,B)),
A \== table,
A \== B,
clear_off(A),
clear_off(B),
on(A,X),
retract(on(A,X)),
assert(on(A,B)),
assert(move(A,X,B)).
% Means there is space on table
clear_off(table).
% Means already clear
clear_off(A) :- not(on(_X,A)).
clear_off(A) :-
A \== table,
on(X,A),
clear_off(X),
retract(on(X,A)),
assert(on(X,table)),
assert(move(X,A,table)).
do(Glist) :-
valid(Glist),
do_all(Glist,Glist).
valid(_).
do_all([G|R],Allgoals) :-
call(G),
do_all(R,Allgoals),!.
do_all([G|_],Allgoals) :-
achieve(G),
do_all(Allgoals,Allgoals).
do_all([],_Allgoals).
achieve(on(A,B)) :-
r_put_on(A,B).
main :-
current_prolog_flag(argv, Argv),
format('Called with ~q~n', [Argv]),
parse the list
listing(on), listing(move),
halt.
main :-
halt(1).
我需要在以下列表中解析输入参数“ cbbaa table”:“ [on(c,b),on(b,a),on(a,table)] ”,以便从规则中执行do (例如do([on(c,b),on(b,a),on(a,table)]))。 格式显示为: Called with [on,b,a,c,b,a,table]
我不是Prolog的专家,我现在真的很困,希望有人可以帮助我。 先感谢您。
你快到了:
:- initialization (main).
main :-
current_prolog_flag(argv, Argv),
parse(Argv, Parsed),
format('Called with ~q~n', [Parsed]),
halt(1).
parse([], []).
parse([X,Y|Argv], [on(X,Y)|Parsed]) :- parse(Argv, Parsed).
保存到名为argv.pl
的文件argv.pl
,我得到以下结果:
$ swipl argv.pl a b c d Called with [on(a,b),on(c,d)]
也就是说,参数对已经实现,并且“程序”终止。 没有错误处理,传递了奇数个参数,我得到一个警告:
$ swipl argv.pl a b c Warning: /home/carlo/test/prolog/argv.pl:1: Initialization goal failed Welcome to SWI-Prolog (threaded, 64 bits, version 7.7.7-2-gd842bce) etc etc...
无论如何,我认为您的main
在成功执行parse / 2之后,应maplist(assertz,Parsed)
retractall(on(_,_))
,然后再进行maplist(assertz,Parsed)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.