访问prolog中列表中的列表元素以进行操作

时间:2014-06-04 16:43:22

标签: list prolog

我一直在努力工作一个小时,现在没有运气。

如果我有一个像这样的列表列表

[[tom, dick, harry], [janice, jane, jess]]

他们有关系

happy(tom).
happy(dick).
happy(harry).
happy(janice).
unhappy(jane).
unhappy(jess).

我想分别查询每个列表,看看他们是不开心还是开心。目前我已经写了两个函数来处理这个问题,它们是

%% Female happiness checker
hcf([]).
hcf([H|T]) :-
   happy(H),
   hcf(T).

%% Male happiness checker
hcm([]).
hcm([H|T]) :-
   happy(H),
   hcM(T).

所以目前这些确实有效。如果我放入

hcm([Janice, jess, jane]).
false

它返回false,因为两个人不高兴。

我遇到的麻烦是写一个函数,它允许我将列表列表中的每个列表传递给每个男性和女性幸福的相应函数。

到目前为止,我已尝试了一些

的内容
%% I  was trying to get a HEAD list and a TAIL list to pass them through but didn't have much luck
happnieschecker([[]])
happnieschecker([[H|T]]) :-
some code here

编辑:我希望程序像这样运行

happinesschecker([tom, dick, harry], [janice, jane, jess]).
false - since jane and jess are unhappy
happinesschecker([tom, dick, harry], [janice]).
true - since everyone is happy

所以我的问题是,我怎样才能将每一个名单的女性和其他所有名单的HCF传递给男性的HCM?

2 个答案:

答案 0 :(得分:2)

happynesschecker(Ms, Fs) :-
   allhappy(Ms), % more compactly:  maplist(happy,Ms).
   allhappy(Fs). 

allhappy([]).
allhappy([P|Ps]) :-
   happy(P),
   allhappy(Ps).

请注意,完全相同的谓词可用于男性和女性列表。

并列出人员名单:

extrahappy(Mss) :-
   maplist(maplist(happy), Mss).

或更详细地说:

extrahappy([]).
extrahappy([Ps|Pss]) :-
   allhappy(Ps),
   extrahappy(Pss).

答案 1 :(得分:1)

prolog列表([a,b,c])是数据结构./2语法糖,其中空列表由 atom {{ 1}}。

  • 列表[][a]
  • 完全相同
  • 列表.(a,[])[a,b]
  • 完全相同
  • 列表.(a,.(b,[]))[a,b,c]
  • 完全相同

我自己,我宁愿使用方括号表示法。你呢?

Prolog的列表符号提供了更多的语法糖,使用.(a,.(b,.(c,[])))运算符将列表分区为 head tail

  • 符号|与`。(a,X)
  • 完全相同
  • 符号[a|X][a,b|X]
  • 完全相同
  • 符号.(a,.(b,X))[a,b,c|X]
  • 完全相同

鉴于此,并假设您的列表列表包含男性和女性的交替子列表:

.(a,.(b,.(c,X)))

这样的事情对你有用

[ [bob,ted] , [carol,alice] , [tom,dick,harry] , [jane,sally,simone] ]

我在这里使用everybody_happy( [] ) . % the empty list is all happy everybody_happy( [Ms] ) :- % a list of length 1 contains a male sublist findall( M , (member(M,Ms),happy(M)) , Ms ) % is everybody happy? . % everybody_happy( [Ms,Fs|Ps] ) :- % a list of length 2+ starts with a male and female sublist findall( M , (member(M,Ms),happy(M)) , Ms ) , % are all the boys happy? findall( F , (member(F,Fs),happy(F)) , Fs ) , % are all the girls happy? everybody_happy(Ps) % is the remainder of the list happy? . % easy! 。如果您想推出自己的幸福检查器,只需将findall/3替换为您的实施:

findall/3

编辑注意:如果你想丢弃第一项,请尝试类似

的内容
everybody_happy( []         ) .  % the empty list is all happy
everybody_happy( [Ms]       ) :- % a list of length 1 contains a male sublist
  is_happy(Ms)                   % is everybody happy?
  .                              %
everybody_happy( [Ms,Fs|Ps] ) :- % a list of length 2+ starts with a male and female sublist
  is_happy( Ms ) ,               % are all the boys happy?
  is_happy( Fs ) ,               % are all the girls happy?
  everybody_happy(Ps)            % is the remainder of the list happy?
  .                              % easy!

is_happy([]).        % the empty list is happy
is_happy([P|Ps] ) :- % a non-empty list is happy IF... 
  happy(P) ,         % - its head is happy, and
  is_happy(Ps)       % - its tail is happy
  .                  % easy!

其中everybody_happy( [_|Xs] ) :- every_body_happy_1( Xs ) . 是实际谓词。