0

这是我的作业代码,它无法正常工作。它应该做的是采用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))).
4

1 回答 1

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).
于 2012-04-27T13:37:07.293 回答