Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

281–290 of 367 posts

Re: Why Does “=” Mean Assignment?

#281
post #86

The Go language brings back the ":=" to mean declare and assign, while inferring type. a := 5 You can also equivalently do var a = 5 I almost never use the latter within functions (only at module scope). I believe the former is forbidden except within functions.

I was puzzled by this decision of theirs though. They are so hell bent on keeping the language as simple as possible with a minimum of keywords, but apparently can't be arsed to just write var a = 5 like normal people, but prefer to invent a whole new operator for it. Operators are often demanding even more of a reason to exist than keywords.

Then you also get the operator inconcistency between "assigning to a new variable" and "assigning to an existing variable" and is it so important to tell the two apart from within an operator, of all things?

When writing Go, I have been way more inconvenienced by its error handling mechanics than having to tell that I want a new variable using a few more characters.

Re: Why Does “=” Mean Assignment?

#282
post #231
post #199

Earlier quoted context omitted.

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.

Though it's neither a tick nor apostroph, but as the parent said a prime: https://en.wikipedia.org/wiki/Prime_(symbol)

Re: Why Does “=” Mean Assignment?

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

The syntax of C is a thing of beauty. The decision to use = for assignment and == for equality was natural, since assignment is more common than equality testing; similarly, articles are usually short words in natural languages. Algol 68 and Pascal got this wrong, and where are they now?

Re: Why Does “=” Mean Assignment?

#284
post #131

Earlier quoted context omitted.

I've used Erlang but not Elixir. Having an additional optional "pin" operator sounds like a bad idea, just piling on complexity. You can't mess up with the Erlang syntax.

The creator of elixir had a blog on this: http://blog.plataformatec.com.br/2016/01/comparing-elixir-an...

only the case argument there is any good. when it get to numbered vars, you know they are grasping for anything

Re: Why Does “=” Mean Assignment?

#286
post #42

Earlier quoted context omitted.

Yeah I was pretty surprised when I found out that assignment and equality can be totally syntactically separate and nonambiguous with miminal language-design effort. Just get rid of the silly idea of allowing expressions as statements. Although even then it's nice to use different symbols because they are different meanings. I don't like it when a word has different meanings depending on context.

Just get rid of the silly idea of allowing expressions as statements. I don't think that's enough. Take the two following Python lines: a = b = c == d a = b == c == d No expression is being used as a statement, yet you can't syntactically separate assignment from equality.

I would write these as

    a = b = (c == d)
and

    a = (b == c == d)
respectively. And I think it would make sense to have a syntax rule that strictly enforces parentheses around truthy expressions used in such contexts.

Re: Why Does “=” Mean Assignment?

#287

It isn't just mathematically unsound, it's the cause of many errors, especially in C. Certainly a "million dollar mistake". A classic way to guard against accidental use of assignment in logic statements is to write it "backwards", i.e.: if (2 == x) {...} Since (2 = x) won't compile, while (x = 2) will not only compile but might appear to work for a very long time.

It isn't mathematically unsound. It is just a terminal symbol in a grammar that follows a different (arbitrary) convention than mathematical notation does. And the errors result from typing "=" when you needed "==" has nothing to do with mathematics. It is a language ergonomics issue resulting from two operators being legal in the same context and one being similar to (a prefix of) the other.

"My writing isn't bad, I just follow different conventions."

The equals sign has been used for hundreds of years in mathematics and it's completely unnecessary to change this meaning.

Re: Why Does “=” Mean Assignment?

#288
post #267
post #261

Earlier quoted context omitted.

Rutishauser might have gotten it from Konrad Zuse's Plankalkül, which used the right double arrow ⇒, which looks a little like =. Plankalkül had the order and the assignment reversed relative to C, so to increment a variable Z1, you'd write: | Z + 1 ⇒ Z V | 1 1 S | 1·n 1·n 1·n (The first line is the 'main line'; Z stands for Zwischenwert, i.e. "intermediate value". The second line is the 'value line', which contains…

What are Strukturindizes?

The translation would be structure indexes.

Re: Why Does “=” Mean Assignment?

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

Or maybe because often used constructs are expressed by shorter symbols, because that makes it easier to write them down?

Re: Why Does “=” Mean Assignment?

#290
post #237

Earlier quoted context omitted.

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.

What is "transiently the same"?

Genuinely asking - my first thoughts are:

"transiently the same" is not the same as "the same", hence it is not "the same", and therefore "not the same".

No jokes on the meta-level please :o)

Post reply on HN