-1

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]
lurker
  • 56,987
  • 9
  • 69
  • 103
tharindya
  • 3
  • 1
  • 1
    And you have tried what? What is it that you did not manage to get to work? –  Mar 03 '14 at 07:10
  • possible duplicate of [Shuffle in prolog](http://stackoverflow.com/questions/8089849/shuffle-in-prolog) – false Mar 03 '14 at 19:08
  • You say that the elements of `List` are partinioned between `List1` and `List2`. Where does `e` in `List1` come from? It does not occur in `List`. – false Mar 04 '14 at 13:17

1 Answers1

0

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 )
      .
    
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • only two cases are needed, not three. See duplicate question above. – false Mar 03 '14 at 19:09
  • Depends on whether you want divide([],As,Bs) to succeed or fail. – Nicholas Carey Mar 03 '14 at 19:11
  • The notion of "partitioning" does not permit this to fail. – false Mar 03 '14 at 19:12
  • OK, now I see what you mean. I meant: you can do it in two clauses with `divide([], Xs, Ys)` succeeding. – false Mar 03 '14 at 19:21
  • If somebody (e.g., the O.P.) is having a hard time wrapping their head around something this simple, we don't need to make their head explode with cleverness :^) – Nicholas Carey Mar 03 '14 at 19:28
  • @NicholasCaray: OTOH, that other solution only needs first-argument, principal functor only indexing. – false Mar 03 '14 at 19:39
  • The OP gave one example for what `dividelist3` means: `dividelist([a,b,c,d],[a,c,e],L).` yields `L=[b,d]`. Your answer yields no solution for `L` in this case. – lurker Mar 04 '14 at 02:07
  • @mbratch: do you really believe that the `e` isn't missing in the first argument (`List`)? How can `e` be in `List1` if it is not in `List`? – false Mar 04 '14 at 13:02
  • @false I'm merely echoing what the OP requested without corrective interpretation. If you interpret their request really as a list difference, then their example could make sense and perhaps their naming of `dividelist` is a misnomer. I think clarification from the OP is required for a final answer on this question. No definition of `dividelist/3` was given except that one example. – lurker Mar 04 '14 at 13:18
  • I cannot see any space of interpretation in "the elements of List are partitioned between List1 and List2". Maybe the precise way how they are partitioned (say every 2nd. or 3rd, or 4th in List1/List2 etc.). – false Mar 04 '14 at 13:21
  • @false given that I've seen many posts on SO with misused terms, I count on any provided examples as helping to clear up that possible question. I didn't want to make that firm assumption without clarification from the OP of their example. Perhaps, since the OP has accepted Nicholas' answer, that the `e` is indeed a typographical error. – lurker Mar 04 '14 at 13:34