R-alpha: returning a list from a function

Jim Robison-Cox (jimrc@mathfs.math.montana.edu)
Fri, 20 Sep 1996 16:31:09 -0600 (MDT)


Date: Fri, 20 Sep 1996 16:31:09 -0600 (MDT)
From: Jim Robison-Cox <jimrc@mathfs.math.montana.edu>
To: R testers <r-testers@stat.math.ethz.ch>
Subject: R-alpha: returning a list from a function

I'm enjoying the new alpha versions.  R has come a long way in a hurry.  
Here's a comment on version 0.20-alpha.

There's a difference between S and R which may be intentional, probably is 
good programming practice, but needs to be noted:

When returning a list of results from a function, the list format seems 
implicit in Splus, not in R:

S:
> test <- function(x) return(len=length(x),xbar=mean(x)) 
> test(1:30)
$len:
[1] 30

$xbar:
[1] 15.5


R:
> test <- function(x) return(len=length(x),xbar=mean(x)) 
> test(1:30)
[1] 30


If I make the list explicit, then both give the same output:
R or S:
> test <- function(x) return(list(len=length(x),xbar=mean(x)) )
> test(1:30)
$len
[1] 30

$xbar
[1] 15.5


This cropped up as I tried to pull Efron And Tibshirani's bootstrap 
functions into R.  bootstrap() returns a list of results, but I was only 
getting the first element. Since some  S code makes use of the explicit 
list form, does it make sense to have R do that, also? Is it hard to 
change return() to check for multiple objects? 

Thanks for all the work on this language!

Jim Robison-Cox                 ____________            
Department of Math Sciences    |            |           phone: (406)994-5340
2-214 Wilson Hall               \   BZM, MT |           FAX:   (406)994-1789
Montana State University         |  *_______|
Bozeman, MT 59717                 \_|         e-mail: jimrc@math.montana.edu 


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- To (un)subscribe, send
subscribe	or	unsubscribe
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-