William Stein wrote:
> On Sun, Aug 2, 2009 at 9:13 AM, Robert Dodier<robert.dod...@gmail.com> wrote:
>> Robert Bradshaw wrote:
>>
>>> Sage lists are Python lists, which are very different than
>>> Mathematica lists.
>> You say that as if it's a fact of geography which can't be changed.
>>
>>> to change all lists would be a massive (backwards-
>>> incompatible) change, as well as another step away from Python.
>> Not exactly. At present something like 1 + [2, 3, 4, 5] causes Python
>> to cough up an error, right? So extending arithmetic operators to
>> lists wouldn't change the behavior of any existing Python program.
>> (I'm not worried that someone might have written a program which
>> requires an error to be triggered when number + list is encountered.)
> 
> Unfortunately for your suggestion, arithmetic operators are methods of
> the objects.  To make "1 + [2,3,4,5]" and "[2,3,4,5] + 1" not raise an
> exception would require changing the Python interpreter itself, at the
> C level.


Maybe for python ints, but surely we can make Sage Integers work without 
raising exceptions, as illustrated on the top portion of 
http://sagenb.org/home/pub/699/ ?  (Of course, keeping in mind that the 
"1" in the examples above is likely to be a Sage Integer, not a python 
int, so we could add (if desired) functions to add/radd the Integer to a 
list.)

Personally, I much prefer list comprehension for a case like this.  In 
fact, I just finished writing an entire reply and example on the 
assumption that [1,2,3]+4 would yield [1,2,3,4], as that was what I 
expected from the current python behavior of [1,2,3]+[4] (i.e., list 
addition is list concatenation!)  Then I realized the proposal was 
something different, so I had to change everything.

Also, someone could define a +eadd+ operator that would be like the +. 
operator in Matlab.  With the code from 
http://trac.sagemath.org/sage_trac/ticket/6245 (appropriately modified 
for "+" precedence), you could do something like the bottom half of 
http://sagenb.org/home/pub/699/, which gives:

sage: [1,2,3] +eadd+ 4
[5, 6, 7]
sage: 1 +eadd+ [2,3,4]
[3, 4, 5]

Thanks,

Jason


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to