Why are there two assignment operators, `<-` and `->` in R?
Asked Answered
S

4

47

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.

Saskatoon answered 26/7, 2018 at 20:56 Comment(7)
Possibly related: softwareengineering.stackexchange.com/questions/98406/…Elin
Shame, my previous comment vanished but I think it’s relevant: the “good writeups” that you linked are inaccurate and misleading. A correct explanation of the differences can be found on Reddit: reddit.com/r/Rlanguage/comments/47c3z7/… — If somebody has an issue with this comment, please let me know why, rather than just flagging it.Archdeacon
so many points in so little time in this thread, where was this posted ?Protease
@Moody_Mudskipper, posted here originally, not moved to SO from a sub-site. I bet a lot of people wonder why R/S is so quirky. I couldn't find any references myself, but thankfully Ben's answer cites Chambers, the primary author of S.Saskatoon
I though it wwas linked from a blog or reddit or something, I was just curiousProtease
@KonradRudolph, thanks for sharing the link. updated question to include the reddit thread.Saskatoon
Are there any functional differences between -> and <-? Except for the perceived difference explained in https://mcmap.net/q/372490/-different-behavior-of-gt-right-arrow-and-lt-left-arrow-on-a-for-loop/7870777 which is not actually a difference but just a difference in perception.Gullet
C
56

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 Brown Book: S: An Interactive Environment for Data Analysis and Graphics R. A. Becker, J. M. Chambers (CRC Press, 1984)
  • Extending the S System, Becker and Chambers (Taylor & Francis, 1985)
  • the Blue Book: The New S Language Becker, Chambers and Wilks (Wadsworth and Brooks/Cole 1988, but reissued in 2018!! by CRC Press)

    1. The Brown Book doesn't mention -> in the main text:

enter image description here

but it does in the appendix:

enter image description here

  1. Extending the S System mentions -> in the main text:

enter image description here

I can't search very much of the book, so -> might also be mentioned in the appendix somewhere.

  1. The Blue Book refers to the right-arrow, but actually seems to have a typo:

enter image description here

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:

enter image description here

If you want a copy of the 1985 book you can get it for $34.41 - or $1070.99 (but with free shipping!) ...

enter image description here

Charlie answered 26/7, 2018 at 22:38 Comment(4)
Thank you for this answer. This design makes perfect sense for interactive-consoles where lines could not be edited. Of course I hadn't even considered that S was developed in the 70s & the available contemporary hardware & software before posting this question. Incidentally, I wonder who would pay $1,070.99 for a copy of Becker: Extending The S SystemSaskatoon
I'm also wondering if the choice to use <- was influenced by the need to have ->. The forward arrow solved a real problem for people working on consoles that didn't have an up arrow to browse the history & allow editing.Saskatoon
I've heard/believe it's documented elsewhere that the keyboards in use at Bell Labs at that times had a dedicated left-arrow key ...Charlie
Informative (+1). You (or Google Books) have a minor typo, using "Chamber" twice when it should be "Chambers"Unsuspected
T
11

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

Thorndike answered 26/7, 2018 at 21:11 Comment(4)
One very minor disadvantage of <- is that its internal implementation is a little weird: str(quote(a->b)) gives language b <- a, i.e. the parser actually flips the expression around when parsing it ...Charlie
Another point is that <- makes it easier keep track of object names. If you're writing expressions that end in -> for assignment, your object names will be horizontally scattered, where as consistently using <- means each object name can be predictably located.Paillasse
Worth to note that the Tidyverse style guide says to not use -> when using pipes.Opulent
@Paillasse Spot on. It’s in fact actively harmful for code quality. Make your side-effects (= assignment) visible, don’t hide them at the end of the line.Archdeacon
A
9

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).

Archdeacon answered 27/7, 2018 at 9:58 Comment(1)
Also POP-2. POP-2 uses an explicit stack; so this would be legal: str.len; f(n+1); 'A' ->Z ->Y ->X which would assign str.len to X, assuming f() returned a single result. More commonly, a function returned multiple results, as in Lua.Hitt
U
7

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.

Unsuspected answered 26/7, 2018 at 21:56 Comment(2)
This is interesting but R doesn’t actually strike me as a language that tried to make matrix operation syntax natural. Compared to Matlab, R’s matrix syntax is positively convoluted.Archdeacon
@KonradRudolph I agree that its syntax isn't optimized for matrices (%*% ??), but it is still true that R is a mathematical programming language which is easy for mathematicians to use. All 3 of the original main developers of S then R (Chambers, Ihaka, and Gentleman) had doctorates in mathematics or statistics. For people with such training, the established use of arrows in linear algebra would make both <- and -> natural choices for assignment. My background is also in math, and I liked <- when I first saw it. And even though I don't use ->, it always struck me as a nice touch.Unsuspected

© 2022 - 2024 — McMap. All rights reserved.