Why Does “=” Mean Assignment?
341–350 of 367 posts
Re: Why Does “=” Mean Assignment?
#342Another 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.
Re: Why Does “=” Mean Assignment?
#343Here'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 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?
#344Earlier 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…
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?
#345Earlier 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.
Re: Why Does “=” Mean Assignment?
#346Re: Why Does “=” Mean Assignment?
#347Earlier 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.
Re: Why Does “=” Mean Assignment?
#348In 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.
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?
#349Earlier 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.
> let {a,b} = {a: 1, b: 2}
> a
1
> b
2Re: Why Does “=” Mean Assignment?
#350I'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)