Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

231–240 of 367 posts

Re: Why Does “=” Mean Assignment?

#231
post #199
post #173

Earlier quoted context omitted.

You have something similar in mathematics. Recurrence relations. Since we are dealing with a sort of time difference inside a computer, something like "x = x + 1" can be interpreted as "the value of x at the next time unit is the value of x at the previous time unit plus one". That is "x[n+1] = x[n] + 1" and this is a recurrence relation. My guess is that early programmers were deeply aware of this time difference, s…

The way these recurrence relations are taught at university is to distinguish the new value from the old value by using a ' pronounced prime. Here, you would write x' = x + 1 to give the recurrence relation x[n+1] = x[n] + 1. Or, more generally x' = f(x) for x[n+1] = f(x[n]). The reason for this is because in mathematics x = x + 1 is absurd (ignoring modulo arithmetic).

In highschool, we used numeric subscripts for that. Ticks(apostrophes) were kept for derivatives.

Re: Why Does “=” Mean Assignment?

#232
post #173
post #170

Earlier quoted context omitted.

This works for initialization, but not for reassignment. Most notably, it is absurd for statements like x = x + 1. Meanwhile, such self-referential updates are very common in imperative programming, so anyone designing the language would come across this.

You have something similar in mathematics. Recurrence relations. Since we are dealing with a sort of time difference inside a computer, something like "x = x + 1" can be interpreted as "the value of x at the next time unit is the value of x at the previous time unit plus one". That is "x[n+1] = x[n] + 1" and this is a recurrence relation. My guess is that early programmers were deeply aware of this time difference, s…

What you have there is a sequence. Recursively defined or not, taking the indices out of a sequence takes away the only thing that makes it a sequence (the mapping from the natural numbers) and would be a horrible abuse of notation.

I think language designers were certainly consciously aware that they were designing something well defined, and chose to use this kind of syntax because its simpler, rather than an implicit mapping to the natural numbers via something like CPU cycles.

Re: Why Does “=” Mean Assignment?

#233
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 "<-".

What does prevent : to become the assignment operator of a language? It's short, and quite "explicit". x: 2

The Rebol family of languages works like that.

Re: Why Does “=” Mean Assignment?

#234
post #165
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 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.

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.

Re: Why Does “=” Mean Assignment?

#235
post #165

Earlier 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.

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.

Re: Why Does “=” Mean Assignment?

#236
post #140
post #127

Earlier quoted context omitted.

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…

To illustrate how close Erlang syntax is to Prolog, here is drop/3 in Prolog: drop(X, List, Result) :- drop(X, List, [], Result). drop(_, [], Acc, Result) :- reverse(Acc, Result). drop(X, [X|Rest], Acc, Result) :- drop(X, Rest, Acc, Result). drop(X, [Y|Rest], Acc, Result) :- dif(X, Y), drop(X, Rest, [Y|Acc], Result). I only had to make a few syntactic changes to the original program to obtain a Prolog predicate from…

Also not stuck using only an unbound result. Could we not ask "given L and Ls, what elements were dropped?"

Re: Why Does “=” Mean Assignment?

#237

Earlier quoted context omitted.

The shorthand I use for "becomes the same as" is "gets".

I never liked the 'gets': "x gets 7" is fine but "x becomes the same as y" seems better than "x gets y"

that's wrong, though. x becomes a copy of y, which is only transiently the same.

Re: Why Does “=” Mean Assignment?

#238
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 "<-".

a byte of RAM or disk is cheaper than a human neuron. Brains are more important than fingers.

Re: Why Does “=” Mean Assignment?

#239
post #119

Earlier quoted context omitted.

Don't compilers warn about assignment inside of conditionals now?

In C++17 not only can you do assignment in the conditional, you can declare new variables which have the scope of the then and else clauses. But yes, most compilers will catch this case.

The compiler warns unless you add an extra () around the assignment to signal that you mean it.

Re: Why Does “=” Mean Assignment?

#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
Post reply on HN