要从列表参数构建其他列表,请对其进行递归访问,并在适当的位置存储元素。看看你能不能完成这个片段
odd_even([], [], []).
odd_even([N|Ns], [N|Odds], Evens) :-
odd(N),
!, odd_even(Ns, Odds, Evens).
% here another odd_even for evens
odd(N) :-
... % use rem/2 to find the integer remainder of N is 1
编辑:要移动列表中偶数位置的元素和另一个列表中奇数位置的元素(通常称为split),访问只是将它们放在两个额外的参数上:我们需要一个终止规则,因为现在我们一起考虑两个参数要拆分的列表。
split([], [], []).
split([X], [], [X]).
split([X,Y|R], [X|Xs], [Y|Ys]) :-
split(R,Xs,Ys).
there is a case to consider: if odd vs even must be 'counted' from the end of list.
Then we should swap (eventually) the lists when done...That would require to add 2 more arguments to the recursive visit, or count the num.of.elements to decide beforehand where to aplce them...