Hacker News new | past | comments | ask | show | jobs | submit login

I'm amazed (and horrified) how come such an intelligent mathematician and designer of a beautiful programming language made such an obvious fuckup of using a commutative operator for string concatenation. Really, I hate python just for this single idiotic notation.

I mean, it's right there in front of your eyes. He talks about the convenience of using a visually commutative operator like "+" for commutative operations, and then a few lines later he says that it is a convenient notation for string concatenation. What. The. Fuck.




I understand the confusion it may cause to a new Python developer who comes from the mathematical background.

Does it cause any problems beyond that? For example, does it interfere with some elegant patterns, or result in some bug prone code, etc?

FWIW, the single main annoyance I've had with python was its treatment of strings as iterables of single character strings. While not wrong in any obvious theoretical sense, it conflicts with the mental model of many developers (both new and experienced) and has probably caused more bugs than any other feature of Python (and even more ugly type checks to avoid such bugs). When people realized it was a problem and discussed removing this feature, Guido said he had tried but (a) it was too much work and (b) he felt the benefits are too great to give up (https://mail.python.org/pipermail/python-3000/2006-April/000...).


> I understand the confusion it may cause to a new Python developer who comes from the mathematical background.

I'm confused mainly because a person with a mathematical background created a language with such an obvious blunder. When I have to use the language (and I do often) I am not confused about string concatenation, I am ashamed of having to use this ridiculous notation.


Yeah, it was a great mind-blown moment for me when I discovered that accessing the first character is idempotent in Python:

s[0] == s[0][0][0][0]


> strings as iterables of single character

While it makes sense to think of them as sequences, this has bitten me more than I care to admit. OT: Is there a name for things we love to complain about, but wouldn't change?


Hmm it does look like the other comment responding to you is correct: + is not always commutative in math: http://mathworld.wolfram.com/OrdinalAddition.html. If mathematicians are fine with that, I'm not sure what argument you may have against this operator being non-commutative in a programming language.


I for one feel that + is a very intuitive choice for concatenating strings and lists. Is there any other common operator you would recommend instead? Because I feel that using a less common one would harm beginner-friendliness significantly.

Also, to the best of my knowledge there is no place in the bible of maths that defines + to be commutative. In particular, addition of (infinite) ordinals is not commutative.


> Is there any other common operator you would recommend instead?

Yes, wedge product[0], written as tilde (~)[1]:

  "a"~("b"~"c") = "abc" = ("a"~"b")~"c" # associative
  "a"~"a"~"a" = "aaa" = "a"^3 # exponentiates with repeat
  -"abc" = "cba" # (with - as string reverse)
  "ab"~"cd" = -(-"cd" ~ -"ab") # weakly[2] anticommutative
If you generalize to regular expressions, you can get some more milage out of + as alternation and * as intersection.

0: https://en.wikipedia.org/wiki/Wedge_product

1: Not caret (^), because that makes a good exponentiate operator.

  2: # vector wedge product is strongly anticommutative
  u~v = -(v~u) = -(-v~-u) # with vector negation


> I for one feel that + is a very intuitive choice for concatenating strings and lists. Is there any other common operator you would recommend instead?

Literally, any other operator would be better than addition. A space, a dot, a product, two dots, a minus sign, whatever. Anything except the visually commutative plus.

> Because I feel that using a less common one would harm beginner-friendliness significantly.

Usage of + for string concatenation is fairly new, probably invented by C++ (which, by the way, uses bit shift operators for output, so it is not really an example of sane language).


“A”+”B” what do you feel should happen? “A”*6 what do you feel should happen?


  "A"+"B" = Regex("A|B") = Regex("[AB]") # or maybe
  "A"+"B" !> [?]Error: cannot convert /A|B/ to type str
  "A"*6 !> TypeError: <str> * <int>


> “A”+”B” what do you feel should happen?

Clearly, that should be undefined. You cannot sum two strings. Unless you are really deranged and you want it to be "C".


> Because I feel that using a less common one would harm beginner-friendliness significantly.

Actually, given that concatenating sequences (strings are an example) is a fairly common operation and for some kinds of sequences, so is adding them elementwise, it would be better not to use a common mathematical operator for concatenation, but to give it its own.


+ is a symbol not an operator. The operator is defined in how you implement. I think its quite a natural thing to use for string concatenation.


> I think its quite a natural thing to use for string concatenation.

Some of you guys have really wicked minds.


Julia does this with *.

IMO it weirds people out


With stardot or just star? Yeah either would be a little weird.

I'm asking partly because Ocaml uses stardot for floating point multiplication.


Just *

"a" * "b" == "ab"

"a" ^ 3 == "aaa"

Though the dot version is actually almost possible because of a special syntax in Julia called broadcasting, which applies any function element-wise.

"a" .* ["a", "b", "c"] == ["aa", "ab", "ac"]

["a", "b", "c"] .* "a" == ["aa", "ba", "ca"]

("a" * ["a", "b", "c"] would raise an error)


> Julia does this with *

A much saner choice. I would prefer a space, still.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: