Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

171–180 of 367 posts

Re: Why Does “=” Mean Assignment?

#171
post #108

I always liked DHH's take on these sorts of arguments (paraphrasing): who the hell cares? Once you know the purpose of the '=' how often do you make mistakes reading or writing code? Whereas Java is all about protecting developers from themselves, Ruby (for example) let's you get away without variable type declaration because at the end of the day, how often do you not know whether a particular variable is a string o…

> how often do you not know whether a particular variable is a string or an integer?

In someone else's code? 100% of the time. I was once a diehard Ruby believer, and I still use it for small programs. But having had the singular displeasure of working on a huge Ruby codebase (pre-JVM Twitter), I have learned, in the hardest possible way, that untyped languages are absolute nightmares when more than one person is involved.

Re: Why Does “=” Mean Assignment?

#172

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…

Not a surprise that the gripes came from the guy who birthed Pascal, seem odd.

Re: Why Does “=” Mean Assignment?

#173
post #170

I think they are forgetting that a lot of languages take que's from old math textbooks where you would see function definitions written as "f(x) = nx + b" or "y = nx + b" and constant assignment with a "c = " notation. if you want to calculate the output of a function into a data table(which is what early computers were often doing) iterating a variable x over a f(x) = xn + b(with n and b being fixed constants) is ex…

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, so "x = x + 1" made perfect sense.

Re: Why Does “=” Mean Assignment?

#174

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.

I've heard the same but with 'becomes' rather than 'gets'. I think becomes works better for people with a mathematical background, where the concept of variables is totally natural. For people without this, 'gets' might be better because it helps anthropomorphize the variable. The coder 'gives' X a value to hold.

Re: Why Does “=” Mean Assignment?

#176
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.

I really like this about pascal. We read it out loud as "becomes the same as", this being what was actually happening. I've carried the habit over to C despite the bare =, it helps me reason and seems to aid in preventing the =/== error.

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

Re: Why Does “=” Mean Assignment?

#177
post #170

I think they are forgetting that a lot of languages take que's from old math textbooks where you would see function definitions written as "f(x) = nx + b" or "y = nx + b" and constant assignment with a "c = " notation. if you want to calculate the output of a function into a data table(which is what early computers were often doing) iterating a variable x over a f(x) = xn + b(with n and b being fixed constants) is ex…

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.

[deleted]

Re: Why Does “=” Mean Assignment?

#178
post #166

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

> 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. "=" means equality in math; a = a + 1 is the same as 1 = 2 because if you subtract a from both sides and add 1 to both sides you get 1 = 2. Lambda calculus has the concept of binding variables, but it doesn't use "=" for that, it uses application of lambda forms. It's t…

Rebinding is essential for recursion, which is a concept in mathematics. Given a fib(x) function defined in terms of itself, the when we express fib(10), the parameter x is simultaneously bound to a number of different arguments through the recursion. We just understand those to be different x's in different instances of the f scope.

Rebinding is also needed for simple composition of operations. Given some f(x), the formula f(3) + f(4) involves x being simultaneously bound to 3 and 4 in different instances of the scope inside f.

Re: Why Does “=” Mean Assignment?

#179
In R, it is actually distinguished that way, example:

a a + 1 -> a # also works, but REALLY bad practice

But, so does

a = a + 1

Granted, there are a bunch of R haters (especially from people with formal CS educations), I think this convention makes a lot of sense. While most will disagree about the 'In case you are wondering, the difference between foo(x = 'value')

x is declared in the scope of the function, whereas:

foo(x x is now declared in the user environment. Granted, that is not good practice, but that is why there is a difference.

Re: Why Does “=” Mean Assignment?

#180
post #53

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 ":=".

FORTRAN was far from the first high level language. Plankalkül for example was out a decade earlier and used →. EX: P1 max3 (V0[:8.0],V1[:8.0],V2[:8.0]) → R0[:8.0] https://en.wikipedia.org/wiki/Plankalk%C3%BCl So, there really was a lot of languages out there, but := was fairly common because it was easy to parse and type.

It didn't actually contribute much to the evolution of other languages though, right? Had anyone even heard of it when Fortran/ALGOL/etc were being designed?
Post reply on HN