Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

splitting lists

13 views
Skip to first unread message

digi....@gmail.com

unread,
Nov 27, 2006, 11:54:31 AM11/27/06
to
just a really basic question...

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]]

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]]]

i know this seems like a simple quesion but i decided to learn prolog
since it seems so different to other languages i have learnt (c/c++,
java) and have just begun to
really get into lists and their operations and i'm curious to know how
this is done.

thanks in advanced.

Markus Triska

unread,
Nov 27, 2006, 12:45:26 PM11/27/06
to
digi....@gmail.com 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

Query Builder

unread,
Nov 28, 2006, 1:10:09 AM11/28/06
to

I also have a question regarding matrix...

I have a 5 x 5 matrix.

A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y

this matrix is passed as a list (A, B, C,D,E,F G,H,I,
J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y).

I need to derive the following:
Row (M) should return ([A,B,C,D,E], [F,G,H,I,J], [K,L,M,N,O],
[P,Q,R,S,T], [U,V,W,X,Y])
Col(M) should return
([A,F,K,P,U],[B,G,L,Q,V],[C,H,M,R,W],[D,I,N,S,X],[E,J,O,T,Y])
Cross(M) should return([A,G,M,S,Y], [B,H,N,T,U], [C,I,O,P,V]....... so
on).

how do I get those functions?

Thx in advance...

Markus Triska

unread,
Nov 28, 2006, 3:23:03 PM11/28/06
to
"Query Builder" <queryb...@gmail.com> writes:

> this matrix is passed as a list (A, B, C,D,E,F G,H,I,
> J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y).

That's not a list; these are terms with functor "," and arity 2:

?- (a,b,c,d) = (a,(b,(c,(d)))).
%@% Success.

> Row (M) should return ([A,B,C,D,E], [F,G,H,I,J], [K,L,M,N,O],
> [P,Q,R,S,T], [U,V,W,X,Y])

In Prolog, you describe relations; for return values in other
languages, you introduce an additional (output) argument in a
predicate:

matrix_rows((A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y),
([A,B,C,D,E], [F,G,H,I,J], [K,L,M,N,O], [P,Q,R,S,T], [U,V,W,X,Y])).

Now you can query:

?- matrix_rows((a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y),Rows).

and get:

Rows = ([a, b, c, d, e], [f, g, h, i, j], [k, l, m, n, o],
[p, q, r, s, t], [u, v, w, x, y])

Analogously for the relation between a matrix and its columns.

Query Builder

unread,
Dec 1, 2006, 4:09:53 PM12/1/06
to
Hi Markus,

Thx for the response. But is there anyway we can have it return one
row/column per enter..

Like this...
ROW = [A,B,C,D,E] ;
when i hit ; it should return the next row..
[F,G,H,I,J] ;
then
[K,L,M,N,O];
and last
[U,V,W,X,Y];

NO....

How do I acheive this..
Thx in advance...

Query Builder

unread,
Dec 1, 2006, 4:15:55 PM12/1/06
to
Pls disregard my earlier question.. I figured it out..!!!!!

ony

unread,
Dec 6, 2006, 9:18:43 AM12/6/06
to

On 28 Нояб., 08:10, "Query Builder" <querybuil...@gmail.com> wrote:
> I also have a question regarding matrix...
>
> I have a 5 x 5 matrix.
>
> A B C D E
> F G H I J
> K L M N O
> P Q R S T
> U V W X Y
>
> this matrix is passed as a list (A, B, C,D,E,F G,H,I,
> J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y).
>
> I need to derive the following:
> Row (M) should return ([A,B,C,D,E], [F,G,H,I,J], [K,L,M,N,O],
> [P,Q,R,S,T], [U,V,W,X,Y])
> Col(M) should return
> ([A,F,K,P,U],[B,G,L,Q,V],[C,H,M,R,W],[D,I,N,S,X],[E,J,O,T,Y])
> Cross(M) should return([A,G,M,S,Y], [B,H,N,T,U], [C,I,O,P,V]....... so
> on).
>
> how do I get those functions?
>

I meeted the same problem during implementing Sudoku game and solved
that by specifying
row([A,B,C,D,E|_],1,[A,B,C,D,E]):-!.
row([_,_,_,_,_|T],N,R):- N>1, N1 is N-1, row(T,N1,R).

col([A,_,_,_,_,
B,_,_,_,_,
C,_,_,_,_,
D,_,_,_,_,
E|_],1,[A,B,C,D,E]):-!.
col([_|T],N,R):- N>1, N1 is N-1, col(T,N1,R).

0 new messages