Another interesting way to think about it is as "match". That is try to match the stuff on the right with the stuff on the left. Take Erlang for example: 1> X = 1. 1 2> X = 2. ** exception error: no match of right hand side value 2 Notice variables are immutable (not just values themselves). Once X becomes 1, it can only match with 1 after that. You might think this is silly or annoying, why not just allow reassignme…
Why Does “=” Mean Assignment?
321–330 of 367 posts
Re: Why Does “=” Mean Assignment?
#322Earlier quoted context omitted.
Though it's true that compilers usually turn assignments into SSA, that's not really the same as what parent was referring to. Static single assignment has the same semantics as normal C-style assignments. The parent was referring to assignments that have (Erlang's) pattern match semantics. Also, there's a simple counterexample. You can have static single assignment that has dynamic multiple assignment (e.g. within a…
>"Though it's true that compilers usually turn assignments into SSA" What is SSA here?
Re: Why Does “=” Mean Assignment?
#323In the mid 60's, when I started programming, most programs had to be keypunched. (There was paper tape entry and Dartmouth's new timesharing system using BASIC, but these weren't in very widespread use.) Until 1964, the keypunch machine in use (IBM 026) had a very limited character set. This is the reason that FORTRAN, COLBOL, and LISP programs were written in upper case. Even the fastest "super" computer of the time, the CDC 6600, was limited to 6-bit characters and didn't have lower case letters.
Naturally, symbols like "←" or "⇐" weren't available, but even the characters ":" and "These early hardware limitations influenced the design of the early languages, and the early programmers all became accustomed to using "=" (or rarely ":=" or SET) as assignment even though "⇐" might have been more logical. The designers of subsequent programming languages were themselves programmers of earlier languages so most simply continued the tradition.
Re: Why Does “=” Mean Assignment?
#324Earlier quoted context omitted.
The point is that ':=' evolved in Algol 68 to solve the ambiguity inherent in '='. C is simply from a less thoughtful and primitive language family and modern languages still seem to be copying C's horrible syntax. It's also probably why technical papers are written in Algol-like pseudocode using the '<-' symbol in LaTeX for assignment.
The syntax of C is a thing of beauty. The decision to use = for assignment and == for equality was natural, since assignment is more common than equality testing; similarly, articles are usually short words in natural languages. Algol 68 and Pascal got this wrong, and where are they now?
Re: Why Does “=” Mean Assignment?
#325Another interesting way to think about it is as "match". That is try to match the stuff on the right with the stuff on the left. Take Erlang for example: 1> X = 1. 1 2> X = 2. ** exception error: no match of right hand side value 2 Notice variables are immutable (not just values themselves). Once X becomes 1, it can only match with 1 after that. You might think this is silly or annoying, why not just allow reassignme…
>"In other languages we might say we have assignment and destructuring ..." I'm not familiar with this term "destructuring." Can you elaborate on the concept? Might you have an example of a destructure operation and a language where its's used?
In nearly every other language, when you call a function, the top of the function typically involves a fair bit of analysis of the arguments to decide what needs to be done.
(That's being generous: typically the entire body of the function revolves around making decisions about what to do, such that you don't know what's going to come out of the function until you've traversed multiple branch options.)
In well-written Erlang, the function itself branches at the top, taking advantage of pattern matching, destructuring, and function heads.
Let's say you're passing around arbitrary data in the form of a tuple that you need to display. Or not, depending on the value of some debug flag.
In Python you might see code like this, assuming that an integer is stored in your program like ('int', 3) and a string might have some arbitrary label, so it's ('str', 'label', 'real string'):
if (debug):
display(x)
def display(x):
if (x[0] == 'int'):
display_int(x[1])
elif (x[0] == 'str'):
display_string(x[1], x[2])
In Erlang, you can branch and destructure at the function head. You don't check the debug flag in the calling code, just pass it along for the function to decide what to do with it; if the flag is false, the function just returns false. (Variables in Erlang are capitalized; the lower-case "strings" in this code are atoms, also called symbols in some languages.) display(_, false) ->
false;
display({int, N}, _) ->
display_int(N);
display({str, Label, String}, _) ->
display_string(Label, String).
Since you can choose the key data elements in your function arguments and branch accordingly in the function head, it becomes immediately obvious looking at the code how many different ways the code can evaluate, and the destructuring in the function heads means you don't have to waste code chunking the data apart before working with it.(Updated: forgot to include the debug flag in the 2nd and 3rd function heads. In all 3 heads, the _ pseudo-variable basically says "I don't care what value we have here". Another option would be to use _Debug in the 2nd and 3rd heads to indicate to the reader what the flag is, or use true since we're expecting that value.)
Re: Why Does “=” Mean Assignment?
#326Earlier quoted context omitted.
>"In other languages we might say we have assignment and destructuring ..." I'm not familiar with this term "destructuring." Can you elaborate on the concept? Might you have an example of a destructure operation and a language where its's used?
The other responses captured the essence of what it is, but I thought it might be interesting to briefly discuss how Erlang uses it. As someone who hasn't been able to use Erlang in his day job for a year now, I sorely miss the language. In nearly every other language, when you call a function, the top of the function typically involves a fair bit of analysis of the arguments to decide what needs to be done. (That's…
Re: Why Does “=” Mean Assignment?
#327Earlier quoted context omitted.
This is from a few years ago, my first time giving it, and I've never had the courage to actually watch it, so ymmv. https://youtu.be/E18shi1qIHU
Dude, this was great. Been looking to get into Erlang, but the "unfamiliarity" always shunned me away. Really nice talk.
Re: Why Does “=” Mean Assignment?
#328Earlier quoted context omitted.
I'm sure I've mentioned it here before, but in my favorite Erlang talk I design the language with the equal sign as my core construct. Since the equal sign is an assertion of truth, and since assertions cause a crash on failure, you can pretty much derive the rest of the language/BEAM characteristics from that.
Thanks for sharing. Enjoyed watching (from the link below). Well done! I like the '=' as an assertion idea. That is, it's like running code with assertions turned on. Never thought about it that way but it makes good sense. Also stealing your idea of "rebootable code" :-)
I almost submitted the talk to That Conference this year. I should start shopping it around more.
Re: Why Does “=” Mean Assignment?
#329So in "x = x + 1", x in its next state is mathematically equal to x + 1 in the state before the assignment
Re: Why Does “=” Mean Assignment?
#330Earlier quoted context omitted.
I think GP might have meant that to go the other way around, i.e. don't allow statements to also be expressions, and more specifically, make assignment a statement.
No, Python assignments are not expressions. "a = b = c" is just an assignment statement with multiple parts, it's not equivalent to "a = (b = c)", which in fact will throw a SyntaxError.