Prolog separating into two lists issue

后端 未结 4 1275
太阳男子
太阳男子 2020-12-12 06:40

I have a prolog assignment.

I need to look at the first item in a list, see if its following items are the same until they are not and separate the lists by the firs

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 07:41

    Here is another solution that takes into account what @Sarah wrote. Given this use, grab/3 should never succeed for an empty list for the first or second argument.

    grab([A], [A], []).
    grab([A,B|Bs], [A], [B|Bs]) :-
       dif(A,B).
    grab([A,A|Xs], [A|As], Bs) :-
       grab([A|Xs], As, Bs).
    
    ?- Xs = [A,B], grab(Xs,Ys,Zs).
      Xs = [A, B], Ys = [A],    Zs = [B], dif(A, B)
    ; Xs = Ys,     Ys = [B, B], Zs = [],  A = B
    ; false.
    

提交回复
热议问题