Questions about arrays

From: Joachim Schimpf <j.schimpf_at_icparc.ic.ac.uk>
Date: Fri 05 Mar 2004 12:02:31 PM GMT
Message-ID: <40486C57.6040604@icparc.ic.ac.uk>
Tomas Uribe wrote:
> This is probably another FAQ, but... Why does the following fail
> (using eclipse 5.7 on Linux)?
> 
> dim(A,[10]), A[3] = 2.

For the same reason that makes

?- 1+1 = 2.

fail, i.e. because 1+1 is the symbolic term +(1,1) which does not unify
with the number 2.  Similarly, A[3] is a short way of writing the symbolic
term subscript(A,[3]) which does not unify with the number 2.

In both cases, these symbolic terms will only be evaluated when you use
them in an "expression" context, e.g. on the right hand side of is/2,
in an arithmetic comparison like =:=/2, or in a constraint like #=/2.

The following will therefore succeed:

?- 1+1 =:= 2.
Yes (0.00s cpu)

?- A = [](4,3,2), A[3] =:= 2.
A = [](4, 3, 2)
Yes (0.00s cpu)

?- dim(A,[5]), A[3] #= 2.		% with lib(ic) or lib(fd)
A = [](_206, _207, 2, _209, _210)
Yes (0.00s cpu)


> 
> In general, how can I initialize an array? My current approach is
> to initialize a list, then use the =.. operation, but I wonder if the
> array notation can be used.

You can traverse an array using a loop:

?- dim(A,[5]), ( foreacharg(I,A) do I=0 ).

A = [](0, 0, 0, 0, 0)
I = I
Yes (0.00s cpu)


> 
> Also, the manual says that the functor name [] is not important, but
> graph_set_nodenames(+Graph, ++NodeNameArray) expects the array to use []
> as its functor, otherwise it gives an "out of range" error (probably best
> classified as a bug in the graph library?).

The statement in the manual is probably misleading. While the operations
that access array elements indeed don't care about the functor, it is still
relevant when you e.g. unify or compare two arrays as a whole, e.g.

?- [](1,2,3) = a(1,2,3).

would obviously not unify. The graph library therefore uses the convention
that this array has to have a []/N functor.


> 
> (I am making a random graph, and giving names to its nodes.)
> 
> Regards,
> 
> - Tomas
> 
> p.s. Let me know if there's a better address to send these
> "not-sure-if-its-a-bug-or-not" type questions!
> 

These are perfectly good questions for the eclipse-users@icparc.ic.ac.uk
mailing list, I'll therefore cc this reply there.


Cheers,
-- 
  Joachim Schimpf              /             phone: +44 20 7594 8187
  IC-Parc                     /      mailto:J.Schimpf@imperial.ac.uk
  Imperial College London    /    http://www.icparc.ic.ac.uk/eclipse
Received on Fri Mar 05 12:10:33 2004

This archive was generated by hypermail 2.1.8 : Wed 16 Nov 2005 06:07:27 PM GMT GMT