dividelist(List,List1,List2)
so that the elements of List are partitioned between List1 and List2. For Example ?
dividelist([a,b,c,d],[a,c,e],L).
L=[b,d]
dividelist(List,List1,List2)
so that the elements of List are partitioned between List1 and List2. For Example ?
dividelist([a,b,c,d],[a,c,e],L).
L=[b,d]
Think about it. It's a pretty simple recursive problem. You've got
The General Case: The source list contains 2 or more elements. Take the first 2 elements from the source list, and place one in each result list. Then, recurse down on the remainder of the source list.
Special Case 1: The source list contains exactly one element. Place it in one of the result lists. Then recurse down on the remainder of the source list (the empty list).
Special Case 2: The source list is empty. Your job is done.
That's all there is to it (and it takes less to write the solution in prolog than it does in English).
Edited to show code:
If dividing the empty list ([]
) should succeed:
divide( [] , [] , [] ) .
divide( [A] , [A] , [] ) .
divide( [A,B|Xs] , [A|As] , [B|Bs] ) :-
divide(Xs,As,Bs)
.
If dividing the empty list ([]
) should fail, just remove the first clause:
divide( [A] , [A] , [] ) .
divide( [A,B|Xs] , [A|As] , [B|Bs] ) :-
divide( Xs , As , Bs )
.