Forums

99 bottles of beer sample programs

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

99 bottles of beer sample programs

Postby Csaba » Wed Apr 15, 2009 1:12 pm

Hi,

Here is some sample program of less importance. The inspiration is http://www.99-bottles-of-beer.net/. Some implementations of “99 bottles of beer” there are elegant, some are short, some are long, some are understandable, some are less understandable and some are funny.

I have written some versions in Cobra. In this code, the numbers could be completed with the toEnglish function so only letters are used and then send to a toCapital function, to capitalize the first letter.

In the bottle99LamdaAndClosureVersion2, I could not write the viewResultingWall closure as do( i) = if (i>0, ‘a’,’b’) lambda function. (EDIT: removed a bad suggestion.)

I leave to those more experienced in Cobra to write a version in a single statement, a version completely in functional style, more elegant versions and some funny versions.

Some versions maybe could be published at http://www.99-bottles-of-beer.net/ when Cobra v0.9 is released?

Have fun with Cobra!

Regards
Csaba
Code: Select all
"""
   99-bottles-of-beer programs in Cobra
   For inspiration see http://www.99-bottles-of-beer.net/
   CsabaU@Hotmail.com
"""
class Bottle99programs
   const maxQuantity = 99

   def bottle99RoughVersion is shared
      for quantity in 99 : 0 : -1  # Python numbers!
         print '[quantity] Bottles of beer on the wall, [quantity] bottles of beer.'
         print 'Take one down and pass it around, [quantity-1] bottles of beer on the wall.'
         print
      print 'No more bottles of beer on the wall, no more bottles of beer.'
      print 'Go to the store and buy some more, 99 bottles of beer on the wall.'

   def bottlesAsString(quantity) as String is shared
      if quantity > 1, return '[quantity] Bottles'
      else if quantity == 1, return 'One bottle'
      else, return 'No more bottles'

   def bottle99FirstCorrectVersion is shared
      for quantity in .maxQuantity : 0 : -1  # Python numbers!
         print '[.bottlesAsString(quantity)] of beer on the wall, [.bottlesAsString(quantity).toLower] of beer.'
         print 'Take one down and pass it around, [.bottlesAsString(quantity-1).toLower] of beer on the wall.'
         print
      print '[.bottlesAsString(0)] of beer on the wall, [.bottlesAsString(0).toLower] of beer.'
      print 'Go to the store and buy some more, [.bottlesAsString(.maxQuantity).toLower] of beer on the wall.'

   def bottle99CompactedVersion1 is shared
      for quantity in .maxQuantity : -1 : -1  # Python numbers!
         print '[.bottlesAsString(quantity)] of beer on the wall, [.bottlesAsString(quantity).toLower] of beer.'
         if quantity > 0
            print 'Take one down and pass it around, [.bottlesAsString(quantity-1).toLower] of beer on the wall. \n'
         else
            print 'Go to the store and buy some more, [.bottlesAsString(.maxQuantity).toLower] of beer on the wall.'

   def bottle99CompactedVersion2 is shared
      for quantity in .maxQuantity : -1 : -1  # Python numbers!
         print '[.bottlesAsString(quantity)] of beer on the wall, [.bottlesAsString(quantity).toLower] of beer.\n' + _
            if(quantity > 0, _
             'Take one down and pass it around, [.bottlesAsString(quantity-1).toLower] of beer on the wall. \n', _
             'Go to the store and buy some more, [.bottlesAsString(.maxQuantity).toLower] of beer on the wall.')

   def verse(quantity) as String is shared
      return '[.bottlesAsString(quantity)] of beer on the wall, [.bottlesAsString(quantity).toLower] of beer.\n' + _
         if(quantity > 0, _
            'Take one down and pass it around, [.bottlesAsString(quantity-1).toLower] of beer on the wall. \n', _
            'Go to the store and buy some more, [.bottlesAsString(.maxQuantity).toLower] of beer on the wall.')

   def bottle99FakedOneLineVersion is shared # sort of....
      for quantity in .maxQuantity : -1 : -1, print .verse(quantity)

   def bottle99ListVersion is shared
      bottlesAsStr  = []
      bottlesAsStr.add( 'No more bottles')
      bottlesAsStr.add('One bottle')
      for quantity in 2 : .maxQuantity+1
         bottlesAsStr.add( '[quantity] Bottles')
      for quantity in .maxQuantity : -1 : -1  # Python numbers!
         print '[bottlesAsStr[quantity]] of beer on the wall, [bottlesAsStr[quantity].toLower] of beer.\n' + _
            if(quantity > 0, _
             'Take one down and pass it around, [bottlesAsStr[quantity-1].toLower] of beer on the wall. \n', _
             'Go to the store and buy some more, [bottlesAsStr[.maxQuantity].toLower] of beer on the wall.')

   def bottle99ArrayVersion is shared
      bottlesAsStrings  = String[] (.maxQuantity+1)
      bottlesAsStrings[0] = 'No more bottles'
      bottlesAsStrings[1] = 'One bottle'
      for quantity in  2 : . maxQuantity+1 
         bottlesAsStrings[quantity] =  '[quantity] Bottles'      
      for quantity in .maxQuantity : -1 : -1  # Python numbers!
         print '[bottlesAsStrings[quantity]] of beer on the wall, [bottlesAsStrings[quantity].toLower] of beer.\n' + _
            if(quantity > 0, _
             'Take one down and pass it around, [bottlesAsStrings[quantity-1].toLower] of beer on the wall. \n', _
             'Go to the store and buy some more, [bottlesAsStrings[.maxQuantity].toLower] of beer on the wall.')

   sig FunctionWithOneIntArgAndReturnsStr( i as int32) as String

   def bottle99LamdaAndClosureVersion is shared
      bottlesAsString as FunctionWithOneIntArgAndReturnsStr = do(quantityHere as int)
         if quantityHere > 1, return '[quantityHere] bottles'
         else if quantityHere == 1, return 'One bottle'
         else, return 'No more bottles'
   
      viewOnWall as FunctionWithOneIntArgAndReturnsStr = do(quantityHere as int) = '[bottlesAsString(quantityHere)] of beer on the wall, [bottlesAsString(quantityHere).toLower] of beer.\n'
      doWork as FunctionWithOneIntArgAndReturnsStr = do(quantityHere as int) = 'Take one down and pass it around, [bottlesAsString(quantityHere-1).toLower] of beer on the wall.\n'
      doHardWork as String = 'Go to the store and buy some more, [bottlesAsString(.maxQuantity).toLower] of beer on the wall.'   
      
      for quantity in .maxQuantity : -1 : -1
         print viewOnWall(quantity)+ if(quantity > 0,  doWork(quantity), doHardWork)
         
   
   def bottle99LamdaAndClosureVersion2 is shared
      bottlesAsString as FunctionWithOneIntArgAndReturnsStr = do(quantityHere as int)
         if quantityHere > 1, return '[quantityHere] bottles'
         else if quantityHere == 1, return 'One bottle'
         else, return 'No more bottles'
   
      viewOnWall as FunctionWithOneIntArgAndReturnsStr = do(quantityHere as int) = '[bottlesAsString(quantityHere)] of beer on the wall, [bottlesAsString(quantityHere).toLower] of beer.\n'
      doWork as String = 'Take one down and pass it around, '
      doShop as String = 'Go to the store and buy some more, '   
      viewResultingWall as FunctionWithOneIntArgAndReturnsStr = do(quantityHere as int)
         if quantityHere > 0,  return'[bottlesAsString(quantityHere-1).toLower] of beer on the wall.\n'
         else, return'[bottlesAsString(.maxQuantity).toLower] of beer on the wall.'

      for quantity in .maxQuantity : -1 : -1
         print viewOnWall(quantity)+ if(quantity > 0,  doWork, doShop) +viewResultingWall(quantity)

   def main is shared
      #.bottle99RoughVersion
      #.bottle99FirstCorrectVersion      
      #.bottle99CompactedVersion1
      #.bottle99CompactedVersion2
      #.bottle99FakedOneLineVersion
      #.bottle99ListVersion
      #.bottle99ArrayVersion
      #.bottle99LamdaAndClosureVersion
      .bottle99LamdaAndClosureVersion2
Csaba
 
Posts: 42

Re: 99 bottles of beer sample programs

Postby todd.a » Sat Mar 06, 2010 10:18 pm

I was going through this website and noticed Cobra was not on there so I submitted a short version. Then after searching the forum realized that this thread existed. Nothing exciting was used, just simple language features.

Code: Select all
class Beer
      
   def main
      for n in 99 : -1 : -1
         if n == 0
            cur = 'No more bottles'
         else if n == 1
            cur = '1 bottle'
            next = 'no more bottles'
         else
            cur = '[n] bottles'
            if n > 2, next = '[n-1] bottles'
            else, next = '1 bottle'

         print '[cur] of beer on the wall, [cur.toLower] of beer.'
         if n == 0, print 'Go to the store and buy some more, 99 bottles of beer on the wall.'
         else, print 'Take one down and pass it around, [next] of beer on the wall.'
         print


Code: Select all
class Beer   
   def main
      for n in 99 : -1 : -1
         if n == 0, cur = 'No more bottles'
         else if n == 1, cur, next = '1 bottle', 'no more bottles'
         else
            cur = '[n] bottles'
            if n > 2, next = '[n-1] bottles'
            else, next = '1 bottle'

         print '[cur] of beer on the wall, [cur.toLower] of beer.'
         if n == 0, print 'Go to the store and buy some more, 99 bottles of beer on the wall.'
         else, print 'Take one down and pass it around, [next] of beer on the wall.'
         print
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Re: 99 bottles of beer sample programs

Postby Charles » Tue Mar 09, 2010 11:49 pm

Come on, Todd. Where's the WPF animated version? :-D
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 89 guests

cron