prolog-打破一个清单

时间:2015-04-04 11:49:07

标签: list prolog

示例是这样的

?- runs([3,4,5,4,2,7,5,6,6,8,3], RunList).
RunList = [[3, 4, 5], [4], [2, 7], [5, 6, 6, 8], [3]]

该列表需要分成许多连续数字的非递减序列。我的代码是:

next([],0).
next([H|_],R):-
    R is H.

runs1([],[]).
runs1([H|T],R):-
    runs1(T,R1),
    next(T,X),
    H=<X,
    R = [H|R1].
runs1([H|T],R):-
    runs1(T,R1),
    next(T,X),
    H>X,
    R = [[H]|R1].

我尝试了很多方法,但仍然不知道怎么写...

希望有人可以帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:-1)

runs([], []):-!.
runs([H|T], S):-
  runs(T, TS),
  ins(H, TS, S).

ins(E, [], [[E]]):-!.
ins(E, [[H|T]|TL], [[E, H|T]|TL]):-
  H >= E, !.
ins(E, TL, [[E]|TL]).