有人可以帮助我处理子列表谓词(Prolog)



我想实现一个谓词子列表1(I1,I2,L,Sub(,它接受列表L并返回Sub,其中包括从索引I1到索引I2的L元素。我的代码一直给我假.知道出了什么问题吗?

sublist1(0,0,[H|T],[H|Sub]).

sublist1(I1,I2,[H|T],Sub):-
   I1 =0, I2>=I1,
   I is I2-1,
   sublist1(I1,I,T,[H|Sub]).
sublist1(I1,I2,[H|T],_):-
  I1>0, I2>I1,
  II1 is I1-1,
  II2 is I2-1,
  sublist1(II1,II2,T,_).

从算法上讲,我会说这应该有效:

% sublist from 0 to 0 should return [H] for any List
sublist1(0,0,[H|T],[H]).
% sublist from 0 to n should return the first element followed with the sublist from 0 to n-1 of the tail
sublist1(I1,I2,[H|T],[H|Sub]):-
    I1 is 0, I2 > 0,
    II2 is I2 - 1,
    sublist1(I1,II2,T,Sub).
% sublist of [H|T] from I1 to I2 should return the sublist from I1-1 to I2-1 of T
sublist1(I1,I2,[H|T],Sub):-
  I1>0, I2>I1,
  II1 is I1-1,
  II2 is I2-1,
  sublist1(II1,II2,T,Sub).

编辑

在线尝试(例如,运行执行并写入sublist1(2,5,[0,1,2,3,4,5,6,7,8],S).(。将alt+91用于[alt+93用于]

最新更新