我需要从列表中的一个元素索引中删除另一个元素索引。所以看起来应该是这样的:
?-delm(2,4,[5,6,-3,6,11,56,81],L),write(L),nl.
L = [5,11,56,81]
所以我已经做到了。
delm(A,B,C,D):-A>B,delm(B,A,C,D).
del1(1,[_|T],T):-!.
del1(N,[X|T],[X|L]):-N1 is N - 1,del1(N1,T,L).
delm(N,2,L,R):-del1(N,L,R),!.
delm(N,M,L,R):-M1 is M - 1,del1(N,L,Buf),delm(N,M1,Buf,R).
length([],0).
length([_|T],N):- length(T,N1),N is N1+1.
?-delm(2,4,[5,6,-3,6,11,56,81],L),write(L),nl.
但我还需要补充一点,如果其中一个数字小于1或大于列表长度,则写入消息("错误")。所以看起来应该是
?-delm(-2,4,[5,6,-3,6,11,56,81],L),write(L),nl.
"Error"
?-delm(2,-4,[5,6,-3,6,11,56,81],L),write(L),nl.
"Error"
?-delm(2,40,[5,6,-3,6,11,56,81],L),write(L),nl.
"Error"
我不知道该怎么做。请帮忙!
答案 0 :(得分:1)
在检查A <= B
:
delm(A, _, _, 'Error') :- A < 1, !.
delm(_, B, C, 'Error') :- length(C, L), B > L, !.
所以整个代码都是(只使用你的代码):
del1(1,[_|T],T):-!.
del1(N,[X|T],[X|L]):-N1 is N - 1,del1(N1,T,L).
delm(A,B,C,D):-A>B,delm(B,A,C,D).
delm(A,_,_,'Error'):-A<1,!.
delm(_,B,C,'Error'):-length(C, L), B>L, !.
delm(N,2,L,R):-del1(N,L,R),!.
delm(N,M,L,R):-M1 is M - 1,del1(N,L,Buf),delm(N,M1,Buf,R).
通常我会称这样的例程为slice
。顺便说一句,length/2
通常内置于大多数prolog环境中。
答案 1 :(得分:1)
&#34;核心&#34;您想要的部分内容可以实现如下:
:- use_module(library(clpfd)).
list_from_to_skipped(Xs,From,To,Ys) :-
From #= From0 + 1,
From #=< To,
N_Skip #= To - From0,
append(Prefix,Xs0,Xs),
append(Skip,Suffix,Xs0),
length(Prefix,From0),
length(Skip,N_Skip),
append(Prefix,Suffix,Ys).
以下是您的示例查询:
?- list_from_to_skipped([5,6,-3,6,11,56,81],2,4,Ls).
Ls = [5,11,56,81].
现在让我们有一个更通用的查询:
?- list_from_to_skipped([a,b,c,d],From,To,Xs).
From = To, To = 1, Xs = [ b,c,d] ;
From = 1, To = 2, Xs = [ c,d] ;
From = 1, To = 3, Xs = [ d] ;
From = 1, To = 4, Xs = [ ] ;
From = To, To = 2, Xs = [a, c,d] ;
From = 2, To = 3, Xs = [a, d] ;
From = 2, To = 4, Xs = [a ] ;
From = To, To = 3, Xs = [a,b, d] ;
From = 3, To = 4, Xs = [a,b ] ;
From = To, To = 4, Xs = [a,b,c ] ;
false.