Re: splitting lists
- From: Markus Triska <triska@xxxxxx>
- Date: Mon, 27 Nov 2006 18:45:26 +0100
digi.empire@xxxxxxxxx writes:
given a list, ex. [1,2,3,0,4,5,6,0,7,8,9,0]
how would i turn this list into a list of lists, wherein the sublists
are the elements separated by the delimiter 0.
so [1,2,3,0,4,5,6,0,7,8,9,0] would then become
[[1,2,3],[4,5,6],[7,8,9]]
Using DCG notation:
split([]) --> [].
split([S|Ss]) --> sub(S), split(Ss).
sub([]) --> [0], !.
sub([E|Es]) --> [E], sub(Es).
Example queries:
?- phrase(split(Ls), [1,2,3,0,4,5,6,0,7,8,9,0]).
Ls = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
?- phrase(split([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), Ls).
Ls = [1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0]
my second question is extending the first one,: given two delimeters
how would i turn the list into a list of lists of lists, so given a
list [2,3,0,4,5,6,0,7,8,9,0,1,2,3,0,4,5,6,0,7,8,9,0,1], delimeted by
first 1 then 0, how would i turn that list into the follow list:
[[[2,3],[4,5,6],[7,8,9]],[[2,3],[4,5,6],[7,8,9]]]
extend([]) --> [].
extend([Ls|Lss]) --> subs(Ls), extend(Lss).
subs([]) --> [1], !.
subs([S|Ss]) --> sub(S), subs(Ss).
Example queries:
?- phrase(extend(Ls), [2,3,0,4,5,6,0,7,8,9,0,1,2,3,0,4,5,6,0,7,8,9,0,1]).
Ls = [[[2, 3], [4, 5, 6], [7, 8, 9]], [[2, 3], [4, 5, 6], [7, 8, 9]]]
?- phrase(extend([[[2, 3], [4, 5, 6], [7, 8, 9]], [[2, 3],
[4, 5, 6], [7, 8, 9]]]), Ls).
Ls = [2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0, ... ]
All the best,
Markus Triska
.
- Follow-Ups:
- Re: splitting lists
- From: Query Builder
- Re: splitting lists
- References:
- splitting lists
- From: digi . empire
- splitting lists
- Prev by Date: splitting lists
- Next by Date: odbc or other database-like interface TO Prolog
- Previous by thread: splitting lists
- Next by thread: Re: splitting lists
- Index(es):
Relevant Pages
|
|