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).
Why Does “=” Mean Assignment?
231–240 of 367 posts
Re: Why Does “=” Mean Assignment?
#232Earlier 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…
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?
#233The 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
Re: Why Does “=” Mean Assignment?
#234Because 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.
Re: Why Does “=” Mean Assignment?
#235Earlier 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.
You’re kind of making the parent’s point.
Re: Why Does “=” Mean Assignment?
#236Earlier 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…
Re: Why Does “=” Mean Assignment?
#237Re: Why Does “=” Mean Assignment?
#238The 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?
#239Earlier 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.
Re: Why Does “=” Mean Assignment?
#240Another 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.