prolog消除了子列表中的第n个元素

时间:2016-01-06 23:22:25

标签: prolog

我想在prolog中编写一个程序,删除前n个长度均匀的子列表。 这就是我所拥有的:

 %calculates the length
 lung([],0).
 lung([_|T],R):-
 lung(T,R1),
    R is R1+1.
 %true if the length is even
 evenLength(T):-
    lung(T,Even),
    Even mod 2 =:=0.
 %true if the length is odd
 oddLength(T):-
    lung(T,Odd),
    Odd mod 2 =\=0.
 %eliminate the n given element from a list
 elim([],_,[]).
 elim([_|T],1,R):-
 elim(T,0,R).
 elim([H|T],N,[H|T1]):-
      N \=1,
      N1 is N-1,
      elim(T,N1,T1).
 %eliminate the n-th given element from a sublist where the length is even
 elimSublist([],_,[]).
 elimSublist([H|T],N,[K|R]):-
      is_list(H),
      evenLength(H), %**
      elim(H,N,K),
      elimSublist(T,N,R).
 %if the length of the sublist id odd than it goes further
 elimSublist([H|T],N,[H|R]):-
      is_list(H),
      oddLength(H),
      elimSublist(T,N,R).
      elimSublist([H|T],N,[H|R]):-
      elimSublist(T,N,R). 

问题在于,例如,如果我写:

 elimSublist([1,2,3,4,[1,2,4,5,6,2],[1,2,3]],2,R).
它给了我:      R = [1,2,3,4,[1,4,5,6 | ...],[1,2,3]]

我知道**应该有问题,但我该如何解决呢?谢谢。

0 个答案:

没有答案