我无法在一小时内完成最后一个问题
reverseDrop(L,R) :-
reverseDrop(L,1,[],R).
reverseDrop([HL|TL],N,I,R) :-
N = 1,
reverseDrop(TL,0,[HL|I],R).
如何完成此代码,因此它会反转列表并删除2个元素中的1个:
?- reverseDrop([3,1,5,2,7,3],L).
L = [ 7, 5, 3 ].
true
?- reverseDrop(['world','a',' hello'],L).
L = [ 'hello', 'world' ].
true
答案 0 :(得分:0)
As StackOverflow is not for doing homework or tests,当时我们都不会做彼此的课堂作业,没有人会在你的时间里提出你的问题!我问Prolog专家(那些已经实现它的人)和这个小宝石回来了,并且为了后来的读者的利益而展示:
reverseDrop([],[]). %this can catch the empty list, when we have run out of data
reverseDrop([Keep|Rest1],Out):- %take the first item in the list off the main input list,
append([_Drop],Rest,Rest1), %removes one item from what is left in the list, to be discarded
reverseDrop(Rest,Reply), % recurse on the remainder
append(Reply,[Keep],Out). % attach the First item to the end of the previous output
reverseDrop([Keep],[Keep]). % this is to catch when a list started with an odd number