Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

241–250 of 367 posts

Re: Why Does “=” Mean Assignment?

#241

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 ass…

In math, "=" is an equivalence relation.

Re: Why Does “=” Mean Assignment?

#242
post #240
post #202

Another question is why the assignment is from right to left when the natural direction would rather be left to right, like 2+2 => x to store the value 4 in x. I was told once by a math professor that it is a habit inherited because we use Arabic numbers/maths which were really meant to be read from right to left. Don’t know if the theory has any merit.

English has Subject-verb-object word order, not object-verb-subject. That's why x is the first word in x = 2+2

> English has Subject-verb-object word order, not object-verb-subject

Imperative programming corresponds to the imperative moodin English, where English normally has verb-object word order with the subject (the entity being commanded) ommitted. The subject of the command to set the value of x to the result of the addition of two and two is the computer/runtime running the code, not the variable x which is the direct object.

English has SVO order for declarative sentences, which correspond to declarative programmig, which tends to feature definition or binding rather than mutating assignment.

Re: Why Does “=” Mean Assignment?

#243
post #5

Because K&R had terrible keyboards so they abbreviated everything as much as possible. Traditionally := was used for assignment, which makes sense since it is an asymmetric symbol for an asymmetric operation.

The article is much more interesting than this comment.

Re: Why Does “=” Mean Assignment?

#244
post #209

Earlier quoted context omitted.

That won't always catch the error and even if it did, why is it better?

-Wall can almost always catch the problem as it will insist on redundant parens surrounding an assignment. So you can only screw it up if you typed extra parens for an equality expression. Variable on left is conventional, it's how people sound it out in their head, so it's just easier to process mentally when the expression becomes more complex. Yoda syntax catches that single error at the expense of making all equa…

Also how do you prevent "if(a=b)" if both sides are variables?

Re: Why Does “=” Mean Assignment?

#245
post #127
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…

Also valid in Erlang: 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…

I have mixed feelings on this. On one hand I did want this notation while learning haskell. Partly because there are a bunch of places in haskell's type system where repeating a variable implicitly gives equality constraints.

On the other hand something like

    drop _ [] = []
    drop x (y:xs)
        | x == y = drop x xs
        | otherwise = y : drop x xs
is often more readable at a glance to me since I don't have to parse variable names to see control flow structures.

Also, in statically typed languages what happens if equality doesn't work for a value.

Re: Why Does “=” Mean Assignment?

#246

Earlier quoted context omitted.

Technical papers are written using '<-' in pseudocode because the papers likely use '=' elsewhere to assert equality in a mathematical statement and they want to avoid confusion.

> avoid confusion You’re kind of making the parent’s point.

Yes, although my intention was to further support their last statement which was left open to a degree.

There is a slight distinction. If you are creating a programming language, you can come up with whatever syntax you'd like for assignment and the user has to learn it. Some choices are better than others if you want your language to be used, but there is a specification for the language that you have written down, either as a human readable document or as the compiler/interpreter. With pseudocode in technical documents, the author typically lacks the space, time, and interest to generate such a specification and leans on mathematical notation to keep things precise.

Re: Why Does “=” Mean Assignment?

#247
post #15

The 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 "<-".

Well, I suppose one could add a `⟵` key to the keyboard... :-P

Re: Why Does “=” Mean Assignment?

#248
post #154
post #10

Earlier quoted context omitted.

There’s also <-, most commonly seen in R but draws it’s heritage from the APL keyboard

In the original Smalltalk implementation, the character corresponding to _ in ASCII was a left facing arrow, which Smalltalk used for assignment at the time.

And now you know where the left-arrow on Commodore keyboards came from (same ASCII value): ASCII-1963, which became PETSCII instead of ASCII-1967.
Post reply on HN