Maxima: lambdas and sublist_indices behaving strangely when acting on symbols



The answer is: lambda quotes its arguments.

Type
? lambda
and you get the documentation and examples about lambda.

I believe the following does what you want.

(%i1) f(l, x) ::= buildq([l : l, x : x, z : gensym()],
sublist_indices(l, lambda([z], z =  x)))$

(%i2) macroexpand(f([x,y,z], a));
(%o2)      sublist_indices([x, y, z], lambda([g33291], g33291 = a))
(%i3) f([x,y,z], a);
(%o3)                                 []
(%i4) f([x,y,z], x);
(%o4)                                 [1]

HTH
Volker van Nek

2012/5/14 Andy L <buggi22 at hotmail.com>

>
> Hello,
> I'm having trouble using sublist_indices on lists of symbols.  Basically,
> I'm trying to implement a function that will find the indices at which a
> given symbol appears within a list of symbols.  In other words, I want a
> function f(l,x) which behaves as follows:
>   f([x,y,z], x) -> [1]  f([x,y,z], y) -> [2]  f([x,y,z], z) -> [3]
> f([x,y,z], a) -> []
> Can anyone explain why the following code should yield [1] instead of []?
>   f: lambda([l,x], sublist_indices(l, lambda([z], z = x)))$  f([x,y,z],
> a);  /* this yields [1] */
> The same function behaves as expected when the arguments are strings:
>   f(["x","y","z"], "a");  /* this yields [], as expected */
> An alternate approach reveals some strange behavior:
>   k(l,x) := block(    [lstrs: map(string, l), xstr: string(x)],
> display(l,x,lstrs,xstr),    sublist_indices(lstrs, lambda([z], z = xstr))
> )$  k([x,y,z], a);
> Here, l is displayed as "[x,y,z]", but lstrs is displayed as "[a,y,z]".  I
> don't understand why "x" gets changed to "a" here, or how to prevent that
> substitution.  (True, I could get around this by just changing the
> parameter "x" to some other unlikely name, like "uncommonDummyVar", but
> that seems fragile & inelegant.)
> Does anyone have any suggestions on how to implement the behavior I'm
> looking for?
>
> Thanks,
> ~ Andrew
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>