[R] Some Programming Humor

Pierre Kleiber pkleiber at honlab.nmfs.hawaii.edu
Wed May 14 19:37:39 CEST 2003


And here's a somewhat inebriated version of the beer bottle program

bottles <- function() {
    outstring <- function(b,s,x) {
      cat(b," ")
      for(i in s) cat(sample(c(i,letters,"",".",","),1,
                           prob=c(x,rep(1,29))))
      cat("\n")
    }
    s1 <- unlist(strsplit("bottle(s) of beer on the wall,",NULL))
    s2 <- unlist(strsplit("bottle(s) of beer.",NULL))
    s3 <- unlist(strsplit("Take one down, pass it around,",NULL))
    s4 <- unlist(strsplit("bottle(s) of beer on the wall.",NULL))
    for (b in 99:1){
      #inbf <- 50+b*9950/99
      #inbf <- 50+9950*exp(-(99-b))
      inbf <- 50+9950/(1+exp(-.1*(b-80)))
      outstring(b,s1,inbf)
      outstring(b,s2,inbf)
      outstring("  ",s3,inbf)
      outstring(b-1,s4,inbf)
      cat("\n")
    }
}

The inebriation function, by which the variable, inbf, is calculated, 
obviously should be adjusted to local cultural beer-drinking norms.
     Cheers, Pierre


Peter Dalgaard BSA wrote:
> "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...) 
> 


-- 
-----------------------------------------------------------------
Pierre Kleiber             Email: pkleiber at honlab.nmfs.hawaii.edu
Fishery Biologist                     Tel: 808 983-5399/737-7544
NOAA FISHERIES - Honolulu Laboratory         Fax: 808 983-2902
2570 Dole St., Honolulu, HI 96822-2396
-----------------------------------------------------------------
  "God could have told Moses about galaxies and mitochondria and
   all.  But behold... It was good enough for government work."




More information about the R-help mailing list