Re: Using difference lists to split a list
- From: "guygu3" <guygu3@xxxxxxxxxxx>
- Date: 28 Dec 2006 14:04:06 -0800
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.
.
- Follow-Ups:
- Re: Using difference lists to split a list
- From: Richard Szopa
- Re: Using difference lists to split a list
- References:
- Using difference lists to split a list
- From: guygu3
- Using difference lists to split a list
- Prev by Date: Using difference lists to split a list
- Next by Date: Re: Using difference lists to split a list
- Previous by thread: Using difference lists to split a list
- Next by thread: Re: Using difference lists to split a list
- Index(es):
Relevant Pages
|
|