LISTS and MATRICES
Author Message
LISTS and MATRICES

I'm working with SWI-Prolog 3.28 and i have a problem:
I want to compare data of matrices:
[a,1,b,2,c,3,d,4] and [X,1,Y,4,Z,2,W,3]
which rows are:
[a,1] --> [X,1]
[b,2] --> [Y,4]
[c,3] --> [Z,2]
[d,4] --> [W,3]
My purpose is that the rows which second term is the same have equal
first term, identifying the variables (X,Y,Z,W...) to the first terms of the
left matrix.

For example:
with [a,1] we must find the row [X,1], and X=a.
with [b,2] we must find the row [Z,2], and Z=b.
with [c,3] we must find the row [W,3], and W=c.
with [d,4] we must find the row [Y,4], and Y=d.

I have made recursions and comparision but I don't
know to output a variable in recursions:

do_it([X|R],Y):-X\=[],do_it([R],Y).

-----------------------------------

I think that all my problems can solve if I run over all of the list having
an output variable of each term of the list for processing it,although it
was temporal, but I don't know do it.

If someone can help me...
Thanks.

Wed, 18 Jun 2003 03:58:29 GMT
LISTS and MATRICES

Quote:

> I'm working with SWI-Prolog 3.28 and i have a problem:
> I want to compare data of matrices:
> [a,1,b,2,c,3,d,4] and [X,1,Y,4,Z,2,W,3]
> which rows are:
>     [a,1] --> [X,1]
>     [b,2] --> [Y,4]
>     [c,3] --> [Z,2]
>     [d,4] --> [W,3]
> My purpose is that the rows which second term is the same have equal
> first term, identifying the variables (X,Y,Z,W...) to the first terms of the
> left matrix.

> For example:
> with [a,1] we must find the row [X,1], and X=a.
> with [b,2] we must find the row [Z,2], and Z=b.
> with [c,3] we must find the row [W,3], and W=c.
> with [d,4] we must find the row [Y,4], and Y=d.

> I have made recursions and comparision but I don't
> know to output a variable in recursions:

> do_it([X|R],Y):-X\=[],do_it([R],Y).

> -----------------------------------

> I think that all my problems can solve if I run over all of the list having
> an output variable of each term of the list for processing it,although it
> was temporal, but I don't know do it.

> If someone can help me...
> Thanks.

If I understand correctly, you do notr want to compare matrices, but to match
each pair of terms.
Because, if you want to compare matrices, you will not be able to "unify"
[_,_,_,2,...] with [_,_,_,4,...].  Look at the term "unification" in your
prolog manual.

What you may want to do (?)

match_list([Variable1,Value1|Remaining1],List2) :-
match(Variable1,Value1,List2),
match_list(Remaining1,List2).

match_list([],_) :- write('End of list'),nl.

match_one(Var1,Val1,[]) :- write('No match for '), write(Var1),nl.

match_one(Var1,Val1,[_,Val2|Remaining2]) :- Val1 \=
Val2,match_one(Var1,Val1,Remaining2).

match_one(Var1,Val,[Var2,Val,_]) :- write(Var1),write(' is matched with
'),write(Var2).

Notes :
1) You must call the two lists with an even number of elements.
2) In the last rule, the same variable "Val" appears twice, that means that
they must unify.  You can replace with an equal condition and two different
variables if it is not what you want.
3) You must call match_list with atoms, DO NOT use variables. Example
match_list([a,1,b,2,c,3,d,4],[w,2,x,4,y,1,z,3]).  If you use capital letters
for variables, then prolog will try to unify and fail because of the numerics.

ELevac

Fri, 20 Jun 2003 09:52:01 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages