It isn't just mathematically unsound, it's the cause of many errors, especially in C. Certainly a "million dollar mistake". A classic way to guard against accidental use of assignment in logic statements is to write it "backwards", i.e.: if (2 == x) {...} Since (2 = x) won't compile, while (x = 2) will not only compile but might appear to work for a very long time.
It's better to use -Wall -Werror options for C compiler.
Why Does “=” Mean Assignment?
121–130 of 367 posts
Re: Why Does “=” Mean Assignment?
#122I don't understand the LISP line at all. I would have said "LET", "SET", and "EQUAL" (since it's talking about numbers). Is there something I'm missing? EDIT: It's since been changed to "let", "set", "equal" (but still lower-case).
I don't understand it either. I suspect the author does not know Lisp, and badly misunderstood an explanation from someone who does. I think that Lisp doesn't even fit well into that table, since putting LET in the first column would implicitly bring in declaration as well, much of the time SET in the second column would be misleading since mutation is so often done implicitly by a looping construct like DO, DOLIST,…
It just happens that there was no consensus yet on how it should work, yet, either!
Then again, neither of the last 2 (modern) programming languages I've used have had FORTRAN/ALGOL-style assignment semantics, so I think the jury's still out.
Re: Why Does “=” Mean Assignment?
#123The length of symbols should roughly be negatively correlated with its frequency. Since in procedural language, assignment is a much more common operation than equality check, it is reasonable to favor "=" over ":=" or even "<-".
also, there's no hand balance on either of the composite symbols. := is my right pinky, <- is different fingers, but bottom and top row. neither exactly flows from the fingertips.
Re: Why Does “=” Mean Assignment?
#124The length of symbols should roughly be negatively correlated with its frequency. Since in procedural language, assignment is a much more common operation than equality check, it is reasonable to favor "=" over ":=" or even "<-".
Re: Why Does “=” Mean Assignment?
#125because in math it means assignment. y = mx+b That is an assignment. There is also a math symbol that specifically designates equivalence.
It should be noted that in mathematics equivalence and equality are not the same thing. Letting ~ represent an equivalence relation on a set X: Equivalence relations have specific properties: x ~ x holds for all x in X x ~ y => y ~ x for all x, y in X x ~ y /\ y ~ z => x ~ z, for all x, y, z in X However, they do not have to be equal , merely equivalent by whatever our relationship requires (though they may be equal)…
http://www.math.harvard.edu/~mazur/preprints/when_is_one.pdf
The punchline:
> In appropriate deference to the manifold ways an object can be presented to us, objects need only be given up to unique isomorphism, this being an enlightened view of what it means for one thing to be equal to some other thing.
Re: Why Does “=” Mean Assignment?
#126I've been programming professionally for 25 years using mostly C inspired languages, and I will still regularly write "if(foo = bar)" on a daily basis and not notice until there's an error. It's easily the most common syntax error I write, followed closely by using commas in my for-loop as apparently it looks like a function to my fingers: "for(x=0, x<10, x++)"
# bad
if v = array.grep(/foo/)
do_something(v)
# some code
end
# good
if (v = array.grep(/foo/))
do_something(v)
# some code
end
Caught a number of nasty bugs that way.[0]: https://github.com/bbatsov/ruby-style-guide#safe-assignment-...
Re: Why Does “=” Mean Assignment?
#127Another 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…
2 = X.
{ok, Y} = X = foo().
Oh, and here's a fun one: 1> {X, X} = {1, 1}.
{1, 1}.
2> {Y, Y} = {1, 2}.
** exception error: no match of right hand side value {1,2}
If you're wondering what the use of that is, here's an Erlang "drop all occurrences of an element X from a list" function. (Prerequisite knowledge for this: Erlang functions have several clause-heads; which one is executed on each call depends on which one is able to successfully bind to—i.e. = with—the arguments.) drop(X, List) -> drop(X, List, []).
drop(X, [], Acc) -> lists:reverse(Acc);
drop(X, [X|Rest], Acc) -> drop(X, Rest, Acc);
drop(X, [Y|Rest], Acc) -> drop(X, Rest, [Y|Acc]).
In other words—first, set up an accumulator. Then, go through the list, and if X can bind in both positions, skip the element; otherwise (i.e. if Y != X) then shift Y into the accumulator. At the end, reverse the accumulator (because you were shifting.)Re: Why Does “=” Mean Assignment?
#128Earlier quoted context omitted.
There’s also <-, most commonly seen in R but draws it’s heritage from the APL keyboard
Fun fact, R also has -> (assign to the RHS instead of LHS), and "super-assignment" >, and also can use = sometimes too (and I think has different semantics). Yay R.
Re: Why Does “=” Mean Assignment?
#129I always liked DHH's take on these sorts of arguments (paraphrasing): who the hell cares? Once you know the purpose of the '=' how often do you make mistakes reading or writing code? Whereas Java is all about protecting developers from themselves, Ruby (for example) let's you get away without variable type declaration because at the end of the day, how often do you not know whether a particular variable is a string o…
Re: Why Does “=” Mean Assignment?
#130The Go language brings back the ":=" to mean declare and assign, while inferring type. a := 5 You can also equivalently do var a = 5 I almost never use the latter within functions (only at module scope). I believe the former is forbidden except within functions.
var a = 5 a, b := 2, 3