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

前端 未结 4 847
我寻月下人不归
我寻月下人不归 2020-12-29 18:31

I know how to use <- and ->, and there are several writeups on the difference between equals assignment & arrow assignment,

4条回答
  •  误落风尘
    2020-12-29 18:56

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

提交回复
热议问题