Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language R

(Vectorized and makes correct lyrics)

Date:10/05/05
Author:Giovanni Millo
URL:n/a
Comments:1
Info:http://www.r-project.org/
Score: (3.01 in 184 votes)
# 99 bottles of beer, alternate R version.
# Demonstrates inherent vectorization of R.
# Creates the correct lyrics, not as in the 
# original BASIC example. 
# Works for any n>2, default n=99. 
# Should work in S-Plus as well.
#
# Giovanni Millo, Trieste (Italy) 4/10/2005
###########################

song<-function(n=99) {
  a<-" bottle"
  b<-" of beer on the wall, "
  c<-" of beer. "
  d<-"Take one down and pass it around: "
  #
  ## set "beer counter" vector
  counter<-c(as.character(n:1),"No more")
  # 
  ## set plural 
  s<-ifelse(counter=="1","","s")
  #
  ## build up the verses vector
  firsthalf<-paste(counter,a,s,b,counter,a,s,c,sep="")
  sechalf1.99<-paste(d,counter[-1],a,s[-1],b,sep="")
  sechalf100<-paste("Go to the store and buy some more: ",
                    counter[1],a,"s",b,sep="")
  ##
  ## song is the vector of n+1 complete verses
  song<-paste(firsthalf,c(sechalf1.99,sechalf100),sep="")
}  

print(song)

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Recursive ApproachRoland01/04/060
Dataframe approachDavid Dailey05/03/080

Comments

>>  Giovanni said on 02/11/08 18:55:46

Giovanni This is most true (and embarrassing!) :^)
I noticed immediately after submitting...

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: