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 C#

(v3.0 Easy Functional Recursive)

Date:08/04/09
Author:Yelinna Pulliti Carrasco
URL:http://toyscaos.tripod.com
Comments:0
Info:http://msdn.microsoft.com/en-us/vcsharp/aa336745.aspx
Score: (2.00 in 4 votes)
using System;
using System.Text;

namespace Bottles_of_Beer
{
    class Program
    {
        delegate int BottlesBeer(BottlesBeer B, int n);

        static void Main(string[] args)
        {
            Func<int, int> BeerEnd = n =>
            {
                Console.WriteLine(
     "Take one down and pass it around, no more bottles of beer on the wall.");
                Console.WriteLine(
     "No more bottles of beer on the wall, no more bottles of beer.");
                Console.WriteLine(
     "Go to the store and buy some more, {0} bottles of beer on the wall.",
                 n + 98);
                return 0;
            };

            Func<int, int> Beer = n =>
            {
                string s = n > 2 ? 
                    String.Concat(
                    "Take one down and pass it around, ",
                    (n - 1).ToString(), " bottles of beer on the wall.") :
                    String.Concat(
                    "Take one down and pass it around, ",
                    (n - 1).ToString(), " bottle of beer on the wall.");

                Console.WriteLine(s);
                return 0;
            };

            BottlesBeer B2 = (b, i) =>
            {
                string s = i >= 2 ? String.Concat(
                    (i).ToString(), " bottles of beer on the wall, ",
                    (i).ToString(), " bottles of beer on the wall.") :
                                String.Concat(
                    (i).ToString(), " bottle of beer on the wall, ",
                    (i).ToString(), " bottle of beer on the wall.");

                Console.WriteLine(s);

                if (i == 1)
                {
                    BeerEnd(i);
                    return 0;
                }

                else
                {
                    Beer(i);
                    return i = b(b, i - 1);
                }
            };

            B2(B2, 99); // you can do: int b0 = B2(B2,99)
            Console.ReadLine();
        }
    }
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
corporate styleveteran corporate coder08/16/090
Concise, using C# 3.5 featuresJeff Dietrich10/26/0714
3Paul M. Parks04/20/059
C# Gratuitously FunctionalPaul Stancer04/14/091
Uses LinqSudipta11/19/100
6Mark Hurley05/31/050
Shows new features of C# 2.0.Bradley Tetzlaff11/24/050

Comments

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: