vivek
vivek

Reputation: 11

How to access and use matrices in prolog

I need random access to matrix locations in prolog (swipl). Suggest me a library that gives a way to create and access random locations in matrix without having to go through the process of processing list.

Upvotes: 1

Views: 1160

Answers (1)

CapelliC
CapelliC

Reputation: 60034

SWI-Prolog has unlimited arity terms, then you don't really need any external library. There are several builtins for access to term' arguments, I find arg is handy. For instance:

?- A = array(1,2,3,4,5,6,7), arg(3, A, X).
X = 3.

Depending on what data you will need to process, and the frequency of access to these matrices, you could apply data hiding, providing yourself with the preferred interface.

For instance, you could need index validation:

?- A = array(1,2,3,4,5,6,7), arg(8, A, X).
false.

Note that no exception is thrown...

arg/3 can also search the matching argument:

?- A = array(1,2,3,4,5,6,7), arg(P, A, 4).
A = array(1, 2, 3, 4, 5, 6, 7),
P = 4 .

edit

in Prolog, assignment must always be handled with care, because the programming model isn't really suited for that. But there are some builtins, like nb_setarg, that do the work. See the relevant documentation section.

Upvotes: 3

Related Questions