Re: splitting lists



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
.



Relevant Pages

  • Re: How do I verify a valid sql statement programatically ??
    ... recordset, ASP, and javascript. ... the ASP session to minimize generating the query from the DB with different ... parts of the system that generated record lists would use the same list ... Many places in our system generate dynamic queries based on various ...
    (microsoft.public.sqlserver.programming)
  • Re: static initialization of arrays
    ... lists) in my baseclass and then combine them in various ways in ... different derived classes. ... My sub-classes send queries to a database. ... With regards to using arrays as opposed to containers for this ...
    (comp.lang.java.programmer)
  • Re: static initialization of arrays
    ... lists) in my baseclass and then combine them in various ways in ... different derived classes. ... My sub-classes send queries to a database. ... With regards to using arrays as opposed to containers for this ...
    (comp.lang.java.programmer)
  • ENOBUFS and DNS...
    ... ENOBUFS being returned to UDP-using applications. ... lots of DNS queries to verify my address. ... solution be for me simply not to send any more mail to the lists? ... takes more time to push out responses than it does for them to come in. ...
    (freebsd-net)
  • Comparing queries
    ... I have a form on which I would like to create two combo boxes, ... with a fixed number of queries in a Value List on both combos. ... query on the 2nd combo, then hit a cmd button to execute a comparison. ... lists of queries. ...
    (microsoft.public.access.forms)