On 3/25/19 7:01 AM, Jonathan Fine wrote:
Chris Angelico asked: what does a negative string look like?
This is a very good question. It looks a bit like a negative number.
>>> 2 + 2 4 >>> len('aa' + 'bb') 4 >>> len(-'bb') -2 # Odd, I must confess. >>> 5 + (-1) 4 >>> len('hello') 5 >>> len(-'o') -1 >>> 'hello' + (-'o') 'hell' >>> len('hello' + (-'o')) 4
Grade school: How can I possible have -3 apples in my bag. University: How can I possibly be overdrawn in my bank account.
Negative strings are similar to negative numbers except: For numbers a + b == b + a For strings a + b != b + a
It is the non-commuting that make negative strings difficult. This is a bit like computer programming. It's not enough to have the correct lines of code (or notes). They also have to be put in the right order.
In the abstract, I believe I understand what Jonathan is saying, and in the concrete, I understand Chris's objection. Ridding a string of some of the graphemes from one end, or the other, or both, or elsewhere, is one or more different operations on the same underlying data type. We just went through this with dictionaries. So what it is "hello" - "world"? "hello" because it doesn't end in "world"? "hello" because it doesn't begin with "world"? "he" because that's "hello" with all the graphemes also in "world" removed? "he" because that's "hello" with all the graphemes also in "world" removed from the end? "hello" because that's "hello" with all the graphemes also in "world" removed from the begining?" And once we pick one of those results, what operator(s) produce the others and don't lead to perl or APL? And no matter how much Python I learn, I still can't divide by zero or by an empty string. ;-)