Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

341–350 of 367 posts

Re: Why Does “=” Mean Assignment?

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

In conventional mathematical notation, as used in science and engineering, formulas usually have the form ν = ε, which is both a statement of equality and a method for calculating ν from the terms in ε. For example, V = I R. If you have V and I and want R, you start by rewriting this as R = V / I. Computer languages explicitly followed this.

Re: Why Does “=” Mean Assignment?

#343

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…

> it overthrows a century old tradition to let “=” denote a comparison for equality

It also, in science and engineering, denotes a method for obtaining lhs when you have values for the terms on rhs. That is the point of converting y = ax² + bx + c to x = (-b ± √(b² − 4ac)) / 2a. Early languages like Fortran explicitly followed that usage.

Re: Why Does “=” Mean Assignment?

#344

Earlier quoted context omitted.

FWIW, that's what Ross Ihaka does as well (or at least did in a presentation that I watched - and pointed out that you can use = for assignment). Personally, I think = for assignment is a disaster, and wish I could use in every language.

I've used R since it was in beta form, and one of the most interesting things to me (aside from the evolution of type hierarchy) has been changes in use of the assignment operator. When I started, = was the norm for assignment, with == for evaluating equality. Later I started noticing that people were recommending I agree = versus == can lead to tricky errors in code, but I still prefer = for various reasons in gener…

Interesting. I started using it in 2000 and was working directly with members of R-core at the time. My experience was that the only time I ever saw = used was by DTL.

It really wasn’t until Google published their style guide and then later when Hadley published his that I started seeing = in heavy usage.

To your point about = being shorter I wonder if the difference was that a lot of the folks I interacted with were using Emacs with ESS which would interpolate _ to Either way I was taught from some of the R creators that <- was evil and to be avoided. It wasn’t until I stopped using R regularly that I switched, it became too mentally taxing to change assignment operators when I bounced between languages

Re: Why Does “=” Mean Assignment?

#345

Earlier quoted context omitted.

If you're speed reading some code 'becomes' is a bit of mouthful.

Do you mouth the name of operators when you read code? Like most people I do make the words sound in my head when reading text but only the variable names make sounds when reading code.

I'm a bit of a mutterer but it depends on my mood I think.

Re: Why Does “=” Mean Assignment?

#347

Earlier quoted context omitted.

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.

There are hundreds of years of tradition in mathematics to support making up your own definitions of things in whatever way is convenient for the domain you are dealing with. There is no reason to expect programming languages to deviate from that tradition.

Re: Why Does “=” Mean Assignment?

#348

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…

`->` is a bad practice? I like to use it at the end of a long `%>%` pipe. Reading the whole thing feels a lot more natural.

The first time I ever saw that you could do that was in a blog post about stringing at the end of the a series of pipes. I thought it was pretty neat (and actually used it a couple times), then had problems when I couldn't figure out why my code was messing up.

for example, this assigns a ggplot to 'plot': df %>% na.omit() %>% ggplot(aes(x=x, y=y)) + geom_line() -> plot

That is really confusing in that the way most people would read it is that it is something to be plotted. However, the assignment does occur and is masked. having 'plot %' as the first line makes it clear that a new object is being created.

We actually had to modify our style guide to prevent the '->'

Re: Why Does “=” Mean Assignment?

#349
post #307

Earlier quoted context omitted.

I could imagine by destructuring he means e.g. unapply in Scala: https://docs.scala-lang.org/tour/extractor-objects.html

Ah OK its a functional construct, this make sense. I have seen this in my brief exposure to Clojure. Thanks for the examples and links.

Recent JS versions have it too:

    > let {a,b} = {a: 1, b: 2}
    > a
    1
    > b
    2

Re: Why Does “=” Mean Assignment?

#350

I've been programming professionally for 25 years using mostly C inspired languages, and I will still regularly write "if(foo = bar)" on a daily basis and not notice until there's an error. It's easily the most common syntax error I write, followed closely by using commas in my for-loop as apparently it looks like a function to my fingers: "for(x=0, x<10, x++)"

This is why I'm a big fan of yoda notation where applicable if (2 == x)

Or simply enable compiler warnings for that...
Post reply on HN