For Programmers: Free Programming Magazines  


Home > Archive > Prolog > December 2006 > splitting lists









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author splitting lists
digi.empire@gmail.com

2006-11-27, 7:01 pm

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

2006-11-27, 7:01 pm

digi.empire@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

2006-11-28, 4:00 am


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 wrote:
> digi.empire@gmail.com writes:
>
>
> 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]
>
>
> 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


Markus Triska

2006-11-28, 7:07 pm

"Query Builder" <querybuilder@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.

All the best,
Markus Triska
Query Builder

2006-12-01, 7:01 pm

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



Markus Triska wrote:
> "Query Builder" <querybuilder@gmail.com> writes:
>
>
> That's not a list; these are terms with functor "," and arity 2:
>
> ?- (a,b,c,d) = (a,(b,(c,(d)))).
> %@% Success.
>
>
> 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.
>
> All the best,
> Markus Triska


Query Builder

2006-12-01, 7:01 pm

Pls disregard my earlier question.. I figured it out..!!!!!


Query Builder wrote:[color=darkred]
> 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...
>
>
>
> Markus Triska wrote:

ony

2006-12-11, 7:10 pm



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).

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com