2

我需要从列表中的一个元素索引删除到另一个。所以它应该看起来像:

?-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"

我不知道该怎么做。请帮忙!

4

2 回答 2

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 环境中。

于 2013-05-30T20:42:09.033 回答
1

您想要的“核心”部分可以按如下方式实现:

:- 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.
于 2015-04-30T17:32:16.837 回答