swi Prolog - 找到家庭结构中儿童的综合工资

时间:2016-11-02 12:35:04

标签: prolog

% family one

family(
   person(pat,marx,date(10,march,1944),unemployed),
    person(charlotte,marx,date(11,february,1946),unemployed),
[
    person(aine,marx,date(17,april,1985),unemployed),
    person(louis,marx,date(25,june,1980),works(harriott,32000)),
    person(pearl,marx,date(10,june,1981),unemployed),
    person(pat_jr,marx,date(11,march,1983),works(world_of_food,50000)),
    person(ricky,marx,date(18,february,1987),unemployed)
]
 ).


% family two

family(
    person(fred,chomsky,date(3,october,1955),works(bean_counters,100000)),
person(sarah,chomsky,date(19,october,1961),works(supercomms, 60000)),
[   person(amos,chomsky,date(1,july,1984),works(sell_cell, 80000))
]
).

我试图找到母亲的名字和姓氏,其母亲的总收入低于100,000。我还希望收回孩子工资的综合收入。

到目前为止,我有这个:

test(name, surname, CombinedIncome) :- 
   family(_,person(name,surname,_,_),Children),
   total_Income([],0]
   total_Income(family(_,_,_,works(_,salary))|Children],Income]
   CombinedIncome <100000

我有点迷失在如何完成访问列表并获得合并后的收入,然后根据100000的价值进行测试

2 个答案:

答案 0 :(得分:1)

以下是从评估的家庭作业中发布问题的一些提示。

  • 更改名称!是的,这听起来很明显,但改变变量的名称,或者在这种情况下伪造的人,会让你从互联网上获得答案变得不那么明显。
  • 更改一些值,或稍微模糊一点。也许我们不需要比较与问题相同的总收入?

希望这些有所帮助,如果你仍然被困住,请记住你可以成对工作,所以也许请别人和你一起工作。继续努力,它现在被推回到星期四。祝你好运。

答案 1 :(得分:0)

编辑根据评论请求,使用predicates children_earning_over_40k/1unemployed_fathers/1扩展了该计划。

% family one
family(
    person(pat,marx,date(10,march,1944),unemployed),
    person(charlotte,marx,date(11,february,1946),unemployed),
    [
        person(aine,marx,date(17,april,1985),unemployed),
        person(louis,marx,date(25,june,1980),works(harriott,32000)),
        person(pearl,marx,date(10,june,1981),unemployed),
        person(pat_jr,marx,date(11,march,1983),works(world_of_food,50000)),
        person(ricky,marx,date(18,february,1987),unemployed)
    ]
).

% family two
family(
        person(fred,chomsky,date(3,october,1955),works(bean_counters,100000)),
        person(sarah,chomsky,date(19,october,1961),works(supercomms, 60000)),
        [   person(amos,chomsky,date(1,july,1984),works(sell_cell, 80000))
        ]
    ).

total_income([], 0).

total_income([person(_,_,_,Work)|R], Total):-
    income(Work, Income),
    total_income(R, RestIncome),
    Total is Income + RestIncome.

income(unemployed, 0).
income(works(_, Income), Income).



mother_with_children_less_than_10k(Name, SurName, CombinedIncome):-
    family(_, person(Name, SurName, _, _), Children),
    total_income(Children, CombinedIncome),
    CombinedIncome > 100000.


children_earning_over_40k(ChildrenOver40k):-
    findall(X ,family(_,_,X), Res),
    flatten(Res, AllChildren),
    children_earning_over_40k(AllChildren, ChildrenOver40k).


children_earning_over_40k([person(Name, _, _, works(_, Salary))|R], [Name|Children1]):-
    Salary > 40000,
    children_earning_over_40k(R, Children1).

children_earning_over_40k([person(_, _, _, unemployed)|R], Children):-
    children_earning_over_40k(R, Children).

children_earning_over_40k([person(_, _, _, works(_, Salary))|R], Children):-
    Salary =< 40000,
    children_earning_over_40k(R, Children).

children_earning_over_40k([], []).

unemployed_fathers(Fathers):-
    findall(X ,family(X,_,_), Fathers1),
    unemployed_fathers(Fathers1, Fathers).

unemployed_fathers([person(Name, _, _, unemployed)|R], [Name | Fathers1]):-
    unemployed_fathers(R, Fathers1).


unemployed_fathers([person(_, _, _, works(_,_))|R], Fathers):-
    unemployed_fathers(R, Fathers).

unemployed_fathers([], []).

mother_with_children_less_than_10k的试运行:

?- mother_with_children_less_than_10k(Name, SurName, CombinedIncome).
Name = charlotte,
SurName = marx,
CombinedIncome = 82000

如果您按;寻求其他解决方案:

?- mother_with_children_less_than_10k(Name, SurName, CombinedIncome).
Name = charlotte,
SurName = marx,
CombinedIncome = 82000 ;
Name = sarah,
SurName = chomsky,
CombinedIncome = 80000.

children_earning_over_40k

的试运行
[debug]  ?- children_earning_over_40k(ChildrenOver40k).
ChildrenOver40k = [pat_jr, amos] .

unemployed_fathers

的试运行
[debug]  ?- unemployed_fathers(UnemployedFathers).
UnemployedFathers = [pat] .