# [Maxima] make a function that assign value to variable and is visible later

Zbigniew Komarnicki cblasius at gmail.com
Tue Jul 17 14:42:52 CDT 2012

```Hello,

I want to obtain such result
A[1]: x(l+2, p+1);
A[2]: x(l+1, p+1);
A[3]: x(l  , p+1);

but by function. How I can do that? I try in this way but this is not optimal
way, I suspect. Is maybe better way?

(%i1) fun(var, j):= create_list( var[i+1] = x(l-i+2, p+j) , i, 0, 2);
(%o1)     fun(var, j) := create_list(var      = x(l - i + 2, p + j), i, 0, 2)
i + 1

(%i2) a: fun(A,1);
(%o2)     [A  = x(l + 2, p + 1), A  = x(l + 1, p + 1), A  = x(l, p + 1)]
1                     2                     3

(%i3) [aL, aR]: [ map(lhs,a), map(rhs,a) ];
(%o3)     [[A , A , A ], [x(l + 2, p + 1), x(l + 1, p + 1), x(l, p + 1)]]
1   2   3

(%i4) aL;
(%o4)     [A , A , A ]
1   2   3

(%i5) aR;
(%o5)     [x(l + 2, p + 1), x(l + 1, p + 1), x(l, p + 1)]

(%i6) map(":", aL, aR);
(%o6)     [x(l + 2, p + 1), x(l + 1, p + 1), x(l, p + 1)]

(%i7) A[1];
(%o7)     x(l + 2, p + 1)

(%i8) A[2];
(%o8)     x(l + 1, p + 1)

(%i9) A[3];
(%o9)     x(l, p + 1)
(%i10)

If I make in this way, this didn't work
fun(var, j):= create_list( var[i+1] : x(l-i+2, p+j) , i, 0, 2);
^^^

(%i1) fun(var, j):= create_list( var[i+1]: x(l-i+2, p+j) , i, 0, 2);
(%o1)      fun(var, j) := create_list(var      : x(l - i + 2, p + j), i, 0, 2)
i + 1

(%i2) a: fun(A,1);
(%o2)      [x(l + 2, p + 1), x(l + 1, p + 1), x(l, p + 1)]

(%i3) A[1];
(%o3)      A
1

(%i4) A[2];
(%o4)      A
2

(%i5) A[3];
(%o5)      A
3

Here are simply symbols A[1], but no assignments is apparent.

How to obtain result as I do in (%i6) by function as assignment that is
visible later e.g.
A[1];
x(l + 2, p + 1)

etc.