I know how to use <-
and ->
, and there are several writeups on the difference between equals
assignment & arrow assignment,
From the answer to an exercise in The New S Language (Becker, Chambers and Wilks 1988), via Google Books:
When you type a long expression only to remember at the end that it would be a good idea to save the result, a right-hand arrow allows you to perform an assignment without retyping the line.
This suggests that S users were working directly in the console, without line-editing capabilities that are available in most modern REPL/interactive environments ...
Some archaeology: I poked around in foundational sources on Google Books. There are three relevant books:
the Blue Book: The New S Language Becker, Chambers and Wilks (Wadsworth and Brooks/Cole 1988, but reissued in 2018!! by CRC Press)
->
in the main text:but it does in the appendix:
->
in the main text:I can't search very much of the book, so ->
might also be mentioned in the appendix somewhere.
I interpret the underlined red passages as supporting that there's a typo in the first underlined line, which should be ->
rather than ← ...
Here's the screenshot of the exercise answer referred to above:
If you want a copy of the 1985 book you can get it for $34.41 - or $1070.99 (but with free shipping!) ...
R is meant to have a syntax which is fairly natural for expressing mathematics. It is interesting to note that ->
is actually common notation in describing some elementary row operations on matrices. For example, s*R_i -> R_i
would be used to denote the operation of replacing row i by s times row i. Something like 2*A[1,] -> A[1,]
is perfectly valid R. I don't know if these considerations had anything to do with the design decision, but it does show that it is a reasonable choice for a mathematical language.
I can’t speculate on R’s reasons for allowing left-to-right assignment. And it’s certainly true that most programming languages (nearly all, in fact) perform only right-to-left assignment.
That said, R isn’t entirely on its own.
I'm not familiar with any other language that allows the right-assignment semantics.
I can think of at least three other (families of) languages that allow it:
Assembly languages often perform left-to-right assignment; for instance, the influential AT&T syntax writes assignment like this:
movl $1, %eax
This assigns the value 1
to the EAX register. (On the other hand, Intel’s x86 syntax performs right-to-left assignment.)
TI-BASIC’s STO (“store”) operation is written like this:
1→A
COBOL uses multiple forms of left-to-right assignment:
MOVE 1 TO x
ADD 2 TO x
etc.
I’m however doubtful whether any of these languages served as inspiration for R’s assignment syntax. By contrast, the APL programming language uses arrow-assignment, and it’s generally accepted that S (and thus indirectly R) took inspiration from that; but APL only performs right-to-left assignment (var ← value
).
I think it is just a matter of personal preference.
Although ->
predated magrittr pipes, one recent use case is that ->
can be used to keep the flow from left to right in such pipes:
library(magrittr)
input %>% fun1 %>% fun2 -> result
On the other hand, given that <-
is mostly used you might want to use <-
even in that case.
The argument for <-
is that it starts the line off with the value being set which is sort of like the purpose of the statement, particularly if the result variable is well named, whereas the right hand side is the mechanics and so is detail subservient to the result -- and one likes to start off with an overview and then only get into the detail later. Below we are defining parameter k
. That it is 3 or whether it is defined by a constant as it is here or a complex expression seems incidental to the purpose of the statement.
k <- 3
Personally, I never use ->
.