Skip to content

A shorthand for '<-'

15 messages · James R. Milks, John Chambers, Simon Urbanek +5 more

#
James,
On Nov 21, 2007, at 8:22 AM, James Milks wrote:

            
That's not a shorthand. `=` and `<-` are semantically different in R.

Alexy was asking presumably about shorthands that you press/type in  
the R Mac GUI which will result in `<-` being produced and those are  
<Alt><-> (this comes from Emacs' "_" shortcut which you type on US  
keyboard as <Shift><-> but given that the character _ is often used  
these days, we created a variant where you use <Alt> instead of  
<Shift>) and <Ctrl><=> (this one is simply a play on the equal sign  
and should make sense on other keyboards, too). The last shorthand I  
mentioned (<Alt>+<=>) is a direct translation of the ? sign (which  
you get in other programs when pressing <Alt><=>) into R code.

Cheers,
Simon
#
I actually like = because I screw myself over with things like
b[b<-1]=-1 on a regular basis. :-)

Of course, I also manage to screw myself over with (b = 1 | x == 2) so
perhaps there's simply no hope for me... Perhaps the Smalltalk-style
assignment ':='....
On Nov 21, 2007 8:45 AM, John Chambers <jmc at r-project.org> wrote:

  
    
#
On Nov 21, 2007, at 11:45 AM, John Chambers wrote:

            
I think we may be talking about different things here.

 > a=list(a=1,b=2)
 > ls()
[1] "a"

 > a<-list(a<-1,b<-2)
 > ls()
[1] "a" "b"

So `<-` and `=` are *not* semantically equivalent (where `<-` and `=`  
represent symbols in the parse tree).

And I suppose the misunderstanding comes from the interpretation of  
`=` and `<-`: I meant them as symbols (which is what I would expect  
since we're talking about writing R code) and you interpreted them as  
operators (which cold be expected given that I used backticks which  
was not wise ;)). You are right that `=` and `<-` are equivalent as  
operators:

 > `=`(a,list(`=`(a,1),`=`(b,2)))
 > ls()
[1] "a" "b"

I hope this makes things even more clear ;).

Cheers,
Simon
#
On Nov 21, 2007, at 1:11 PM, Byron Ellis wrote:

            
Wasn't that ALGOL, really? ;) I must agree that I like it as well (and  
surprisingly it's still compatible with R's syntax - just in case we  
don't have enough assignment operators already) - but then, I'm one of  
those poor kids who grew up on Pascal, so I don't count ;). [Still, I  
wouldn't go as far as to re-introduce begin/end :P]

Cheers,
Simon
#
On Nov 21, 2007, at 9:19 PM, Simon Urbanek wrote:
[...]
Guys, thanks for extremely illuminating discussion!  I always liked  
'<-' more than '=' and am staunchly typing it.  And of course there's  
a -> form.  This is beautifully asymmetric.  Now I can type <- in  
Emacs with '_' (in ESS) and in R.app with ?-.  (Now if only I find  
how to do it in TextMate!...  :)

Cheers,
Alexy
#
On Nov 21, 2007 10:27 AM, Simon Urbanek <simon.urbanek at r-project.org> wrote:
I always thought it was a common mapping for the 3270's left arrow key
(I also always assumed that's where S' <- operator came from) when
PC-style keyboards started dominating, the same as the ^ operator in
Smalltalk is a mapping to the up arrow key on the original keyboard. I
too grew up on Pascal (Turbo Pascal 3. Imagine my disappointment in
Anders Hejlsberg and C#, though it's now finally starting to Suck
Less).

  
    
#
Oh, just check out the TextMate R bundle, it's in there.
On Nov 21, 2007 10:32 AM, Alexy Khrabrov <deliverable at gmail.com> wrote:

  
    
#
On Nov 21, 2007, at 9:36 PM, Byron Ellis wrote:

            
I have it, and it talks to R.app beautifully -- but didn't find <-  
there.  So wrote a snippet!  Yay!

Cheers,
Alexy
#
On 11/21/2007 1:19 PM, Simon Urbanek wrote:
Of course, even using them as operators the parser doesn't think they 
are identical:

 > if (a <- 1) cat("yes\n")
yes
 > if (a = 1) cat("yes\n")
Error: unexpected '=' in "if (a ="
 > if (`=`(a,1)) cat("yes\n")
yes

Duncan Murdoch
#
Duncan Murdoch wrote:
Ouch!  That's a good example of why people complained when "=" was added 
as an operator.  In your first expression, "=" is being used in two 
quite different ways.  (You can make out a case that argument 
specification is a little like assignment, since it creates an object of 
that name in the environment of the call, but that's pretty subtle.)

In a sense "=" inside a function call is not semantically an operator at 
all; it's absorbed by the parser into the internal structure that 
represents the call.  In contrast, putting an assignment operator inside 
an argument to a function is very different (and, IMO, something to be 
avoided in writing clear S-language code.)

The difference can be highlighted by looking at the parsed but 
unevaluated expressions in the two cases:

 > e1 = quote(list(a=1,b=2))
 > e1[[2]]
[1] 1
 > names(e1)
[1] ""  "a" "b"
 > e2 = quote(list(a<-1, b<-2))
 > e2[[2]]
a <- 1

The "=" has in a sense disappeared, only affecting the names attribute 
of the language object.
Again, the parser prohibition comes from catching a classic programming 
error in C (and another complaint about using "=" as an assignment 
operator).

while(flag = 0) { // of course, I meant (flag == 0)
  ...
  if(something(...)) flag = 1;
}

after which the programmer wonders why the loop never exits.

(These points recall some surprisingly religious arguments 10 years or 
so ago ;-))
#
Hi,
John Chambers wrote:
[...]
Not only in S or R. I would say this should be avoided in any language
where the order in which the function args are evaluated is undetermined,
especially in situations like this one:

  b <- 0
  f(a <- b + 1, b <- 7) # will call f(1, 7) or f(8, 7)?

[...]
You mean, the loop will never enter.

Cheers,
H.
3 days later
#
Hi List,

Since upgrading to Leopard, the font (or just style?) of my plot  
titles has changed. They used to be bold but are no longer. I tried to  
force it using:

plot(1,1, main=expression(bold("title")))

... but there's no difference to

plot(1,1, main="title")

Am I missing a font or has something else changed? I need the old look  
since I'm in the middle of a long document and I'd like consistency  
with the plots I've made up to now.

Cheers,

Demitri