regular behaviour ?




On Sun, 16 May 2010, Robert Dodier wrote:

< On 5/15/10, Mario Maio <mario.maio at aruba.it> wrote:
< 
< > Sorry for the probable trivial question, I'm an occasional user. I'd
< > just like to know if the following behaviour is normal:
< >
< > (%i1) a[2]:45;
< > (%o1)                                 45
< > (%i2) a;
< > (%o2)                                  a
< > (%i3) a:[1,2,3];
< > (%o3)                              [1, 2, 3]
< > (%i4) a[2];
< > (%o4)                                 45
< > (%i5) a[1];
< > (%o5)                                 a
< >                                         1
< 
< Well, the initial a[2]:45 creates a so-called undeclared array
< (i.e. a hash table) named a.
< [1, 2, 3] is a list, which is distinct from an undeclared-array,
< and it is assigned as the value of a.
< It's possible in Maxima to have different kinds of things
< associated with the same symbol -- Maxima is a "Lisp-2"
< in language war terminology. So an expression like a[1] or
< a[2] has to be disambiguated -- from the evidence it appears
< Maxima has chosen the undeclared array over the value of a
< (a list in this case).
 
Just to follow on, I think Maxima's behaviour here is fairly
understandable if you think of the first statement as defining
a look-up function named 'a' that knows only one answer for one
input value. Since [] is the accessor for a lookup function,
when you ask 'a' what is its value for the input 1, it returns
a[1], i.e. it is undefined. 

Note that if you define a list
b:[1,2,3];
without defining a lookup function named 'b', then [] will work
as you expect.

If you want to reliably access a list element, use
part (or first, second, etc.). 

Leo

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.