I know how to use <-
and ->
, and there are several writeups on the difference between equals
assignment & arrow assignment, but I don't know when to prefer ->
over <-
.
It seems the community has coalesced around using <-
for assignment.
Neither the google R style-guide, nor Hadley Wickam's tidyverse R style-guide even mention ->
in the assignment section.
I'm curious about the design considerations that led the S/S-PLUS developers to put in the right arrow assign operator ->
. In what setting(s) would using ->
be considered more readable (or easier to type) versus <-
or =
?
I'm not familiar with any other language that allows the right-assignment semantics. What languages inspired R in this regard?
I'm looking for answers that cite books / early design documents / user manuals / archived mailing lists or other references to establish what the S author/designer's intent was in putting in the forward-arrow assignment operator.
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 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:
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:
COBOL uses multiple forms of left-to-right assignment:
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
).From the answer to an exercise in The New S Language (Becker, Chambers and Wilks 1988), via Google Books:
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 like2*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 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: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 parameterk
. 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.Personally, I never use
->
.