Re: Using difference lists to split a list




guygu3 wrote:
Hi all,
I have been asked to write a pridicate using a difference list which
takes a list L and split it to the middle which in first half goes to
list L1 and the second goes to L2.
I have an idea how to do it with regular lists, but the slitest idea
about making it with difference lists.
Help!!!

Ok guys,
Help me please.
the split I wrote is:

split(L-X,N-V,L-X,0).
split([X|Xs]-B,L1-A2,A3-C,N):-!,
N>0,
N1 is N-1,
split(Xs-B,[X|L1]-L1,A3-C,N1).


And this is the trace:

[trace] 72 ?- split([a,b,c,d|T]-T,L1-[],L2-[],2).
Call: (7) split([a, b, c, d|_G588]-_G588, _G593-[], _G596-[], 2) ?
creep
^ Call: (8) 2>0 ? creep
^ Exit: (8) 2>0 ? creep
^ Call: (8) _L177 is 2-1 ? creep
^ Exit: (8) 1 is 2-1 ? creep
Call: (8) split([b, c, d|_G588]-_G588, [a|_G593]-_G593, _G596-[], 1)
? creep
^ Call: (9) 1>0 ? creep
^ Exit: (9) 1>0 ? creep
^ Call: (9) _L203 is 1-1 ? creep
^ Exit: (9) 0 is 1-1 ? creep
Call: (9) split([c, d|_G588]-_G588, [b, a|_G593]-[a|_G593],
_G596-[], 0) ? creep
Exit: (9) split([c, d]-[], [b, a|_G593]-[a|_G593], [c, d]-[], 0) ?
creep
Exit: (8) split([b, c, d]-[], [a|_G593]-_G593, [c, d]-[], 1) ? creep
Exit: (7) split([a, b, c, d]-[], _G593-[], [c, d]-[], 2) ? creep

T = []
L2 = [c, d] ;
Redo: (9) split([c, d|_G588]-_G588, [b, a|_G593]-[a|_G593],
_G596-[], 0) ? creep
^ Call: (10) 0>0 ? creep
^ Fail: (10) 0>0 ? creep
Fail: (8) split([b, c, d|_G588]-_G588, [a|_G593]-_G593, _G596-[], 1)
? creep
Fail: (7) split([a, b, c, d|_G588]-_G588, _G593-[], _G596-[], 2) ?
creep

No.



Thnks.

.



Relevant Pages

  • Using difference lists to split a list
    ... I have been asked to write a pridicate using a difference list which ... I have an idea how to do it with regular lists, but the slitest idea ... about making it with difference lists. ...
    (comp.lang.prolog)
  • Question about difference lists
    ... As an exercise, I wrote a predicate ... "unique" to find all the unique elements in a list. ... predicate using difference lists. ... difference lists, the member/2 predicate will find a variable in the ...
    (comp.lang.prolog)
  • Re: What is (not) a closed list?
    ... I think the terminology covering the topic could be 'incomplete data ... evaluation (googling for 'difference lists', ... I presume a standard defn for a list/1 test would be ...
    (comp.lang.prolog)
  • Re: Using difference lists to split a list
    ... I have an idea how to do it with regular lists, ... In terms of difference lists, ... pair) and leaving the minuend as it was. ...
    (comp.lang.prolog)
  • Re: Using difference lists to split a list
    ... I have an idea how to do it with regular lists, ... In terms of difference lists, ... pair) and leaving the minuend as it was. ...
    (comp.lang.prolog)