Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

141–150 of 367 posts

Re: Why Does “=” Mean Assignment?

#141
My first programming teacher insisted we call the single = sign the 'gets' operator. So

int x = 10 int y = x

would read in English as x gets 10, y gets x.

Shortly after that class I took a break from any coding. When I went to another school I saw Softmore and Junior level programmers still struggling with this. I retained the habit of using 'gets'and never had a problem with this.

Re: Why Does “=” Mean Assignment?

#142

Here'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…

At one point in my career, I was afflicted with a Wirth-designed language. His opinion on what constitutes good language design does not carry much weight with me. In particular, I can tell you that the extra typing of Pascal over C really does matter over a couple of years. And that := for assignment is a major pain when your left pinky finger is out of action for weeks, and you still need to hit shift for every assignment.

But then we get to this line:

> Because it overthrows a century old tradition to let “=” denote a comparison for equality

In what sense is "=" for comparison a century old? Hasn't it been used for "things that are equal" for multiple centuries? If so, and if that's distinct from "for comparison", then isn't for comparison also a new, non-standard use?

Does anyone understand what he's on about in this quote?

Re: Why Does “=” Mean Assignment?

#143
post #26

Earlier quoted context omitted.

Traditionally? I don't think there was a "tradition". ":=" was used in Algol. "=" was used in Fortran. COBOL didn't use either "=" or ":=".

From the article: > Looking at this as a whole, = was never “the natural choice” the assignment operator. Pretty much everybody used := for assignment instead, possibly because = was so associated with equality. Nowadays most languages use = entirely because C uses it, and we can trace C using it to CPL being such a clusterfuck.

> From the article:

Articles, like desserts, aren't always right. I am sorely tempted to pull out Sammet's book tonight and start counting.

Re: Why Does “=” Mean Assignment?

#144

because in math it means assignment. y = mx+b That is an assignment. There is also a math symbol that specifically designates equivalence.

I sympathize with this view; I did some simple programming in C-like languages before I learned algebra, and it's sometimes hard to not see = this way. But if = in math notation denoted "assignment", what could ≠ mean?

Maybe lhs = rhs is the set of assignments implied by a postcondition. So x = 5 implies a single assignment, but x = ±5 could mean two possible assignments. Similarly, x ≠ 5 would mean all the possible assignments but x = 5.

Re: Why Does “=” Mean Assignment?

#145
post #13

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…

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" :-)

Re: Why Does “=” Mean Assignment?

#146

Here'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…

> Because it overthrows a century old tradition 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…

You're missing the point - languages like Pascal have a := assignment operator because the = operator is for comparison only. If you accidentally drop the :, then you have a simple Boolean expression. If this Boolean expression is used as a statement, by itself, then the result is a compiler error.

Re: Why Does “=” Mean Assignment?

#147
>A common FP critique of imperative programming goes like this: “How can a = a + 1? That’s like saying 1 = 2. Mutable assignment makes no sense.” This is a notation mismatch: “equals” should mean “equality”, when it really means “assign”.

I sort of disagree with this. Many functional languages pull heavily from lambda calculus and other forms of mathematics. In math, "a = a + 1" isn't the same as "1 = 2". The issue isn't equality, it's that you're trying to rebind a bound variable, which isn't possible.

In other words, rebinding a bound variable is not the same as "1 = 2".

Re: Why Does “=” Mean Assignment?

#148

I've always had the notion that if I made a language I'd make the operators := and == so it'd be harder to accidentally use the wrong one.

Wouldn't : and = make it equally (heh!) hard to use the wrong operator notation?

Using == and := would guarantee you never accidentally use = by itself (out of habit from other languages) because that's not a valid operator.

Re: Why Does “=” Mean Assignment?

#149
When I was in 10th grade, I found a Pascal book(I live in Vietnam so no internet at the time), I have no computer too. I though it was a math book since I aware of Pascal.

I was super confuse when I saw:

i := i + 1

I'm not sure how I get used to it but when I discover Erlang I feel so much happy since we nolonger has that in Erlang.

I wish more language follow it.

Re: Why Does “=” Mean Assignment?

#150

Earlier quoted context omitted.

Wait...LET was optional? Auigh! Whole months of my childhood, wasted!

Depends on the dialect. It was mandatory on the Sinclair BASICs, for example (ZX81/ZX Spectrum), but optional on the Amstrad (Locomotive/Mallard) and BBC BASICs. A quick search suggests it was optional as early as "MicroSoft"'s Altair BASIC: http://swtpc.com/mholley/Altair/BasicLanguage.pdf As someone who learned first on a ZX81, I'd always assumed that "LET X=5" was the canonical form, and that "X=5" for assignment…

Likewise - but of course in Sinclair BASIC it was mandatory because every command had to begin with a keyword - a fact enforced by the fact that the cursor started out as a flashing [K], and every key on the keyboard was mapped to directly enter a keyword in that mode. LET was on the 'L' key. You literally couldn't type "a = 1" into a Spectrum/ZX81 - you couldn't type an 'a' unless you were at an [L] cursor prompt, which would only appear after you had chosen a keyword. Typing an 'a' at a [K] cursor would get you the keyword NEW.

I wonder if you, like me, were also completely thrown when you first encountered a programming language where you didn't also have to enter line numbers...

Post reply on HN