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#

(C# Gratuitously Functional)

Date:04/14/09
Author:Paul Stancer
URL:https://www.ohloh.net/p/ximura
Comments:1
Info:n/a
Score: (2.96 in 48 votes)
using System;
using System.Collections.Generic;
using System.Linq;

namespace Bottles99
{
    /// <summary>
    /// A gratuitously functional implementation of the 99 bottle of beer on the wall program.
    /// </summary>
    static class Program
    {
        static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
        {
            foreach (var item in items) action(item);
        }

        static void Main(string[] args)
        {
            Console.WriteLine("99 bottles of beer on the wall, 99 bottles of beer.");

            Func<int, bool, string> bottle = (v, upper) => 
            {
                return string.Format("{0} bottle{1}", 
                    (v == 0) ? ((upper ? "N" : "n") + "o more"):v.ToString() , ((v == 1) ? "" :
"s"));
            };

            Enumerable.Range(0, 99).Reverse()
                .ForEach(i =>
                {
                    Console.WriteLine(string.Format(
                        "Take one down and pass it around, {0} of beer on the wall.\r\n", 
                            bottle(i, false)));
                    Console.WriteLine(string.Format(
                        "{0} of beer on the wall, {1} of beer.", 
                            bottle(i, true), bottle(i, false)));
                });

            Console.WriteLine("Go to the store and buy some more, 99 bottles of beer on the
wall.");

            Console.ReadLine();
        }
    }
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Concise, using C# 3.5 featuresJeff Dietrich10/26/0714
2Paul M. Parks04/20/059
corporate styleveteran corporate coder08/16/090
4Mark Hurley05/31/050
Uses LinqSudipta11/19/100
Shows new features of C# 2.0.Bradley Tetzlaff11/24/050
v3.0 Easy Functional RecursiveYelinna Pulliti Carrasco08/04/090

Comments

>>  Tom B said on 06/02/09 04:20:24

Tom B Here's the best I could come up with. It uses C# 1.0 features only.

I think this best illustrates the structure of the song without any of my own personality leaking through.

class Beer {
static void Main()
{
const string X0 = "{0}";
const string X213 = "{2}{1}{3}";
const string X4c = "{4}, ";
const string X4p0 = "{4}.\r\n";
const string X4p = X4p0 + "\r\n";
const string X5 = ".\r\n{5}, ";
const string x6 = "n{6}";
const string X6 = "N{6}";
const string F = X0 + X213 + X4c + X0 + X213 + X5;
const string F0 = X0 + X213 + X4p + F;
const string FN = x6 + X213 + X4p + X6 + X213 + X4c + x6 + X213 + X5 + "99" + X213 + X4p0;
for(int i = 99; 0 <= i; i--)
{
System.Console.Write(
(i == 99) ? F : (i == 0) ? FN : F0,
i,
(i == 1) ? null : "s",
" bottle",
" of beer",
" on the wall",
(i == 0) ? "Go to the store and buy some more" : "Take one down and pass it around",
"o more";);
}
}}

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: