I'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++)"
Why Does “=” Mean Assignment?
91–100 of 367 posts
Re: Why Does “=” Mean Assignment?
#92Here's what Niklaus Wirth (Pascal, Modula-2, Oberon) said about using the equal sign for assignment: > A notorious example for a bad idea was the choice of the equal sign to denote assignment. It goes back to Fortran in 1957 and has blindly been copied by armies of language designers. Why is it a bad idea? Because it overthrows a century old tradition to let “=” denote a comparison for equality, a predicate which is…
And if the goal of mathematicians and programmers were the same, this might be a compelling argument. In no sense do I see my programs as giant algorithms, they're machines, and as such require different syntax to construct.
> But mixing up assignment and comparison is a truly bad idea
Then := is also a bad decision. You should use the fully historically supported form of 'let = '. Both := and == suffer from the fact that if you omit a single character, you change assignment into comparison or vice versa.
It strikes me as a nitpicky argument for it's own sake.
Re: Why Does “=” Mean Assignment?
#93Re: Why Does “=” Mean Assignment?
#94Earlier quoted context omitted.
Here's a PDF about running BCPL on a Raspberry Pi: http://www.cl.cam.ac.uk/~mr10/bcpl4raspi.pdf It's aimed at teaching programming to 10 year olds no prior experience. One of the first examples is implementing RSA.
Implementing a cryptosystem is one of the first things for 10 year olds with no prior programming experience???? That seems intense compared to Scratch, etc.
Re: Why Does “=” Mean Assignment?
#95I 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 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, DOTIMES or whatever (no SET in sight), and there are all sorts of equality tests one could use.
Edit: ok, I see the author has both updated it, and is specifically talking about Lisp 1.5 which does not have the looping constructs. On the other hand, it at least has a bunch of alternatives to SET that probably should be listed as well, like RPLACA, RPLACD, ATTRIB, and so on. I think that even in Lisp 1.5 variables could be declared in different ways to using LET (like rebinding a variable passed in to a function, so the assignment was in invocation).
Re: Why Does “=” Mean Assignment?
#961 => x
[ 10 11 12 13 ] => mylist
Reads and evaluates left to right. C seems backwards after that. (Also LISP-type lists without commas).
Re: Why Does “=” Mean Assignment?
#97Re: Why Does “=” Mean Assignment?
#98because in math it means assignment. y = mx+b That is an assignment. There is also a math symbol that specifically designates equivalence.
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).A common example that I would expect most CS students to have encountered is the idea of "congruent modulo n".
We would say that `x ~ y (mod n)` iff `(x mod n) = (y mod n)`.
So over the whole of the integers we can see that every odd number is equivalent/congruent to each other over 2. But we would not say that they are equal to each other.
On the other hand, we would say that 1/4 is equal to 2/8 (or `1/4 = 2/8`).
Re: Why Does “=” Mean Assignment?
#99Earlier quoted context omitted.
Wait...LET was optional? Auigh! Whole months of my childhood, wasted!
Depended on the dialect. Whatever we had on my first PC as a kid, it was mandatory. Later, in HS, when we used BASIC for a first CS course, it was optional.
Re: Why Does “=” Mean Assignment?
#100It 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.