From: Rainer Schöpf <rainer.schoepf@gm...>  20110715 19:51:28

On Tue, 12 Jul 2011 at 18:13 +0100, Arthur Norman wrote: > On Tue, 12 Jul 2011, Ted Kosan wrote: > > > Is it possible to multiply all of the members in a list by a value in > > a manner which is similar to this example from MathPiper?: > > > > In> 3 * {a,b,c} > > Result: {3*a,3*b,3*c} > > > > > > 1: a := {x,y,z}; > > a := {x,y,z} > > 2: for each p in a collect 3*p; > > {3*x,3*y,3*z} > > 3: > > One could imagine trying to find where the "Illegal operatio on lists" is > produced and turn > V op {A,B,C} > into > {V op A, V op B, V op C} > and similarly if the list and item were the other way around. With care so > that > {a,b}*{c,d} > gave > {{a,b)*c, {a,b}*d} > and hence > {{a*c, b*c}, {a*d, b*d}} > if that would be the right way round... It is trickier than that. Try this: 1: a:={x,y,z}; a := {x,y,z} 2: 3*a+3; ***** Illegal operation on lists 3: 3+3*a; ***** a invalid as scalar The problem here that the expression analysis (in getrtype) is incomplete, so that the expression 3*a+3 is recognized as one containing a list, but 3+3*a is not. (More precisely, for addition and subtraction, the first operand determines the type, where as for products and quotients, it looks through all the operands until it finds a nontrivial type. I'm at a loss to explain why this is the case.) > > Also, is there a predicate function like NUMBERP that determines if an > > expression is a list and that can be used in algebraic mode? The > > IsList function is used in MathPiper for this and here is an example > > of it being used for reference: > > > > In> IsList(a) > > Result: False > > > > In> IsList(4) > > Result: False > > > > In> IsList(2*a) > > Result: False > > > > In> IsList({a,b,c}) > > Result: True > > > > Looking at packages/rlisp/list.red to see how "first" etc are implemeted > for lists providing a listp function should be easy... This can be done with the arglength and part operators. part(x,0) returns the toplevel operator of expression x. One needs to guard against atomic expressions, so that algebraic procedure islist l; arglength l > 1 and part(l,0)=list; is a suitable definition. (Note that the name listp cannot be used; there exists a symbolic procedure of the same name already.) > Also that file would be where one hacked in support for other operations > over lists... I might feel the need to think for a bit about whether > anything could be broken by installing those things but they may be > safe... you can look at list.red and see what you thing! More generally, one should have various boolean operators for testing type, at least for those types than can be declared, like matrix, vector, etc. Rainer 