[R] Some Programming Humor

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed May 14 14:59:36 CEST 2003


"Ian Wilson" <i.wilson at maths.abdn.ac.uk> writes:

> Douglas Bates wrote:
> > Douglas Bates <bates at cs.wisc.edu> writes:
> > 
> > 
> >>I checked and R is one of the 515 languages mentioned in the enclosed.
> > 
> 
>   Very nice. But where's the S version? :)
> 
> The "R" version used a loop - so it could not really be called S ;)
> 
> How about
> 
> cat(paste(n <- 99:1,a <- "bottles of beer",b <- "on the wall,\n"
> ,n,a,"take one down, pass it around,\n",n-1,a,b,"\n"))

Do we guarantee left-to-right evaluation?

Here's my old version:

f <- function(n) structure(c(paste(c(n,n,n-1),
     "bottles of beer",c("on the wall","")),
     "take one down, pass it around")[c(1,2,4,3)], class="verse")
print.verse <- function(x,...) cat(x, sep="\n")
lapply(99:1, f)

(It's an open question what to strive for in these examples. My tack
was that it should highlight features of the language, so I threw in
lapply, (S3) methods, and smart indexing...) 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list