这是我的作业代码,它无法正常工作。
它应该做的是以第一行代码中所见的family(Title,[Name/Speed|_])
形式获取家庭事实,并用手电筒产生众所周知的桥拼图的所有可能结果。然而,它所说的都是真或假,取决于家庭事实是否存在。
如果所有事实都是例如
person(dad,1)
person(mom,2)
person(kid,5)
person(granny,10)
当^在代码中,并且最后assert_family
被删除时代码是好的。
所以这与assert_family
没有被正确使用有关。
family(original, [father/1,mother/2,child/5,granny/10]).
moveFamily(Key,Journey, TotalTime):-
assert_family(Key),
findall(Person+Time, person(Person, Time), Left),
moveFamily(Left, [], Journey),
findall(Time, member([Time|_], Journey), LTime),
sumlist(LTime, TotalTime).
moveFamily([P1-T1, P2-T2], _, [[T, [P1-P2]]]):-
T is max(T1, T2).
moveFamily(Left, Right, [[LT, [P1-P2]],[RT, [P3]]|Journey]):-
select(P1-T1, Left, MLeft1),
select(P2-T2, MLeft1, MLeft2),
LT is max(T1, T2),
select(P3-RT, [P1-T1,P2-T2|Right], MRight),
moveFamily([P3-RT|MLeft2], MRight, Journey).
:- dynamic person/2.
assert_family(Key) :-
family(Key,List),
forall(member(N/V,List),
assertz(person(N,V))).
答案 0 :(得分:0)
% this seems to work:
% LPA WIN-PROLOG 4.010 on mac with WinOnX
family(original, [father/1,mother/2,child/5,granny/10]).
moveFamily(Key,Journey, TotalTime):-
assert_family(Key),
findall(Person-Time, person(Person, Time), Left), % changed+
moveFamily(Left, [], Journey),
findall(Time, member([Time|_], Journey), LTime),
sumlist(LTime, TotalTime).
moveFamily([P1-T1, P2-T2], _, [[T, [P1-P2]]]):-
T is max(T1, T2).
moveFamily(Left, Right, [[LT, [P1-P2]],[RT, [P3]]|Journey]):-
select(P1-T1, Left, MLeft1),
select(P2-T2, MLeft1, MLeft2),
LT is max(T1, T2),
select(P3-RT, [P1-T1,P2-T2|Right], MRight),
moveFamily([P3-RT|MLeft2], MRight, Journey).
%:-dynamic(person/2).
assert_family(Key) :- dynamic(person/2),
family(Key,List),
forall(member(N/V,List),
assertz(person(N,V))).
select(H,[H|T],T).
select(X,[H|T],[H|T1]):-select(X,T,T1).
sumlist(L,Sum) :-
sumlist(L,0,Sum).
sumlist([],Sum,Sum).
sumlist([H|T],Count,Sum) :-
NewCount is Count + H,
sumlist(T,NewCount,Sum).