Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

101–110 of 367 posts

Re: Why Does “=” Mean Assignment?

#101
post #71

> Prolog came out the same year as C and popularized logic programming, and you can sort of fake assignment with cuts. Can anyone explain to me what this is supposed to mean? (I know Prolog, I understand cuts very well. But I don't know what the author means here.)

It means I don't understand Prolog very well and really shouldn't have namedropped it :P

Prolog is a good example of a language where "=" doesn't mean assignment. In Prolog, "=" means unification: You can read X = Y (which is =(X, Y) in functional notation) as: "True iff X and Y are unifiable". You can think of unification as a generalization of pattern matching. For example, f(X, b) is unifiable with f(a, Y) by the substitution {X → a, Y → b}. I think this aspect would be a valuable addition to the article.

Fun fact: If (=)/2 were not available as a built-in predicate in Prolog, you could define it by a single fact:

    X = X.
Thus, you do not even need this predicate as a predefined part of the language.

Re: Why Does “=” Mean Assignment?

#102
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…

Prolog's unification: X = Y, X = 5, %% Y = 5 now as well -- ?- append([1,2], Y, [1,2,3]). Y = [3].

Yeah, Prolog's unification operator really blows your mind the first few times you experience it. You left the best part out of that last example:

    ?- append(X, Y, [1,2,3]).
    X = [],
    Y = [1, 2, 3] ;

    X = [1],
    Y = [2, 3] ;

    X = [1, 2],
    Y = [3] ;

    X = [1, 2, 3],
    Y = [] ;

    false.
(a few newlines added for clarity)

Re: Why Does “=” Mean Assignment?

#104

because in math it means assignment. y = mx+b That is an assignment. There is also a math symbol that specifically designates equivalence.

it’s not an assignment it’s expression of a relationship, and you can rewrite it in many equivalent ways. You can’t coherently rewrite an assignment that way.

Re: Why Does “=” Mean Assignment?

#106
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…

Some(all?) compilers transform your code into something like this internally. Its called static single assignment form. https://en.wikipedia.org/wiki/Static_single_assignment_form

Though it's true that compilers usually turn assignments into SSA, that's not really the same as what parent was referring to. Static single assignment has the same semantics as normal C-style assignments. The parent was referring to assignments that have (Erlang's) pattern match semantics.

Also, there's a simple counterexample. You can have static single assignment that has dynamic multiple assignment (e.g. within a loop construct). Under the pattern-matching semantics, evaluating the assignment (or rather the pattern match) a second time could fail, whereas an assignment always succeeds.

Re: Why Does “=” Mean Assignment?

#107

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

> "=" was used in Fortran. And most BASICs, though some (most?) required the keyword LET to introduce an assignment, as well.

Let became optional quickly it seems

But most BASIC versions would accept 'let a = 1'

Personally, I don't see why the big fuss. If your assignment and comparison operators are the same the only thing you lose is doing x = (y == 2); kind of expressions (since you would disambiguate from conditional expressions).

Now, assignments are done more frequently than comparisons, hence why it makes sense for it to be the simpler/shorter one.

Re: Why Does “=” Mean Assignment?

#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 or an integer? Enough to justify enforcing declaring everything at the outset or receiving errors throughout your code?

That's not to say that those enforcements never make sense. I think that structured languages like Java are better for larger teams where enforcing standards is more important than a smaller team. But there are other ways to do that and a lot of time the rules make development a horrible experience.

Re: Why Does “=” Mean Assignment?

#109

because in math it means assignment. y = mx+b That is an assignment. There is also a math symbol that specifically designates equivalence.

No it is not. Have you ever studied logic? Equality is a relation. Assignment does not make much sense if you don't elaborate what you're trying to say. For example, we can assign 1 := S(0), 2 := S(S(0))... in the metatheory so that we can meaningfully use 1, 2 ... in the theory. It is also sometimes denoted with =_{def} and some books use = for this type of assignment and \dot{=} (= with dot on top) for the equality relation inside the theory. Either way, you're not right.

Re: Why Does “=” Mean Assignment?

#110
post #14

> Since assignment is about twice as frequent as equality testing in typical programs, it’s appropriate that the operator be half as long. Ken Thompson on why '=' is assignment and '==' the equality check. It's this kind of mindset that puts me off Go. But I can totally see that many who want a better C getting into Go exactly for reasons like this.

But := is not "assignment" per se in Go, it is 'declare and assign'. Something like auto x = 2; in modern C++ (or C#);

If you declare a variable, then x = 2; works in Go.

Post reply on HN