Prolog:过滤目标列表并保留成功的目标
Prolog: Filtering a list of goals and keeping the successful goals
给出以下事实和限制:
car(red, 2000).
car(black, 1990).
car(blue, 2010).
millennials(car(_,Y)) :- (car(_,Y)), Y =< 1995.
red(car(C,_)) :- car(C,_), C == red.
给定一个列表 [millennials, red]
,我想拨打这样的电话:
checkRestriction([millennials, red], car(red, 2000) , L).
L = red
其中 return 汽车所遵守的条件列表作为参数传递。
这与此线程不同:,因为不必所有限制都成功,目标只是 return 成功限制的列表,并将汽车作为参数传递.
我尝试了以下方法:
checkRestriction([],_,[]).
checkRestriction([H|T], Car, List) :-
( H(Car)
-> append(L, H(Car), List),
checkRestriction(T, Car, List)
; checkRestriction(T, Car, List) ).
哪个检查是限制 returns true with car 作为参数,并且在这种情况下将限制本身附加到列表中,否则只是简单地用列表的尾部调用谓词,但我得到编译错误。
参见库中的 include/3 谓词(应用)。
https://www.swi-prolog.org/pldoc/doc/_SWI_/library/apply.pl?show=src
目标 H(Car)
导致编译错误,因为变量不能用作 Prolog 中的谓词名称。要更正您的代码,您必须按如下方式构造目标项:
?- H = red, Car = car(red,2000), Goal =.. [H,Car].
H = red,
Car = car(red, 2000),
Goal = red(car(red, 2000)).
?- H = millennials, Car = car(red,2000), Goal =.. [H,Car].
H = millennials,
Car = car(red, 2000),
Goal = millennials(car(red, 2000)).
因此,正确的代码是:
checkRestriction([], _, []).
checkRestriction([H|T], Car, Result) :-
Goal =.. [H, Car],
( call(Goal)
-> Result = [H|List],
checkRestriction(T, Car, List)
; checkRestriction(T, Car, Result) ).
millennials(car(_,Y)) :- car(_,Y), Y =< 1995.
red(car(C,_)) :- car(C, _), C == red.
blue(car(C,_)) :- car(C, _), C == blue.
black(car(C,_)) :- car(C, _), C == black.
运行 例子:
?- checkRestriction([millennials, red], car(red,2000), L).
L = [red].
?- checkRestriction([millennials, black], car(black,1990), L).
L = [millennials, black].
?- checkRestriction([millennials, blue], car(blue,2010), L).
L = [blue].
?- checkRestriction([millennials, black], car(blue,2010), L).
L = [].
或者,您可以使用 call/N
:
checkRestriction([], _, []).
checkRestriction([H|T], Car, Result) :-
( call(H, Car)
-> Result = [H|List],
checkRestriction(T, Car, List)
; checkRestriction(T, Car, Result) ).
给出以下事实和限制:
car(red, 2000).
car(black, 1990).
car(blue, 2010).
millennials(car(_,Y)) :- (car(_,Y)), Y =< 1995.
red(car(C,_)) :- car(C,_), C == red.
给定一个列表 [millennials, red]
,我想拨打这样的电话:
checkRestriction([millennials, red], car(red, 2000) , L).
L = red
其中 return 汽车所遵守的条件列表作为参数传递。
这与此线程不同:
我尝试了以下方法:
checkRestriction([],_,[]).
checkRestriction([H|T], Car, List) :-
( H(Car)
-> append(L, H(Car), List),
checkRestriction(T, Car, List)
; checkRestriction(T, Car, List) ).
哪个检查是限制 returns true with car 作为参数,并且在这种情况下将限制本身附加到列表中,否则只是简单地用列表的尾部调用谓词,但我得到编译错误。
参见库中的 include/3 谓词(应用)。 https://www.swi-prolog.org/pldoc/doc/_SWI_/library/apply.pl?show=src
目标 H(Car)
导致编译错误,因为变量不能用作 Prolog 中的谓词名称。要更正您的代码,您必须按如下方式构造目标项:
?- H = red, Car = car(red,2000), Goal =.. [H,Car].
H = red,
Car = car(red, 2000),
Goal = red(car(red, 2000)).
?- H = millennials, Car = car(red,2000), Goal =.. [H,Car].
H = millennials,
Car = car(red, 2000),
Goal = millennials(car(red, 2000)).
因此,正确的代码是:
checkRestriction([], _, []).
checkRestriction([H|T], Car, Result) :-
Goal =.. [H, Car],
( call(Goal)
-> Result = [H|List],
checkRestriction(T, Car, List)
; checkRestriction(T, Car, Result) ).
millennials(car(_,Y)) :- car(_,Y), Y =< 1995.
red(car(C,_)) :- car(C, _), C == red.
blue(car(C,_)) :- car(C, _), C == blue.
black(car(C,_)) :- car(C, _), C == black.
运行 例子:
?- checkRestriction([millennials, red], car(red,2000), L).
L = [red].
?- checkRestriction([millennials, black], car(black,1990), L).
L = [millennials, black].
?- checkRestriction([millennials, blue], car(blue,2010), L).
L = [blue].
?- checkRestriction([millennials, black], car(blue,2010), L).
L = [].
或者,您可以使用 call/N
:
checkRestriction([], _, []).
checkRestriction([H|T], Car, Result) :-
( call(H, Car)
-> Result = [H|List],
checkRestriction(T, Car, List)
; checkRestriction(T, Car, Result) ).