The Twelve Days of Christmas: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Perl 6}}: Restore Perl solution)
(→‎{{header|Erlang}}: Add implementation)
Line 1,211: Line 1,211:
Two turtle doves and
Two turtle doves and
A partridge in a pear tree</pre>
A partridge in a pear tree</pre>

=={{header|Erlang}}==
<lang erlang>-module(twelve_days).
-export([gifts_for_day/1]).

names(N) -> lists:nth(N,
["first", "second", "third", "fourth", "fifth", "sixth",
"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" ]).

gifts() -> [ "A partridge in a pear tree.", "Two turtle doves and",
"Three French hens,", "Four calling birds,",
"Five gold rings,", "Six geese a-laying,",
"Seven swans a-swimming,", "Eight maids a-milking,",
"Nine ladies dancing,", "Ten lords a-leaping,",
"Eleven pipers piping,", "Twelve drummers drumming," ].

gifts_for_day(N) ->
"On the " ++ names(N) ++ " day of Christmas, my true love sent to me:\n" ++
string:join(lists:reverse(lists:sublist(gifts(), N)), "\n").

main(_) -> lists:map(fun(N) -> io:fwrite("~s~n~n", [gifts_for_day(N)]) end,
lists:seq(1,12)).
</lang>
{{Out}}<pre>On the first day of Christmas, my true love sent to me:
A partridge in a pear tree.

On the second day of Christmas, my true love sent to me:
Two turtle doves and
A partridge in a pear tree.

[...]

On the twelfth day of Christmas, my true love sent to me:
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.</.pre>



=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==

Revision as of 15:21, 22 December 2015

Task
The Twelve Days of Christmas
You are encouraged to solve this task according to the task description, using any language you may know.

Write a program that outputs the lyrics of the Christmas carol The Twelve Days of Christmas. The lyrics can be found here. (You must reproduce the words in the correct order, but case, format, and punctuation are left to your discretion.)

Cf

ActionScript

This program outputs the lyrics to a TextField object. The text field can be scrolled using the mouse wheel (Windows only) or by using the up/down arrow keys on the keyboard.

Works with: Flash Player version 10
Works with: AIR version 1.5

(Although the code can work in Flash Player 9 by replacing the Vectors with Arrays)

<lang ActionScript>package {

   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.KeyboardEvent;
   import flash.events.MouseEvent;
   import flash.text.TextField;
   import flash.text.TextFieldAutoSize;
   import flash.text.TextFormat;
   import flash.ui.Keyboard;
   
   public class TwelveDaysOfChristmas extends Sprite {
       
       private var _textArea:TextField = new TextField();
       
       public function TwelveDaysOfChristmas() {
           if ( stage ) _init();
           else addEventListener(Event.ADDED_TO_STAGE, _init);
       }
       
       private function _init(e:Event = null):void {
           
           removeEventListener(Event.ADDED_TO_STAGE, _init);
           
           _textArea = new TextField();
           _textArea.x = 10;
           _textArea.y = 10;
           _textArea.autoSize = TextFieldAutoSize.LEFT;
           _textArea.wordWrap = true;
           _textArea.width = stage.stageWidth - 20;
           _textArea.height = stage.stageHeight - 10;
           _textArea.multiline = true;
           
           var format:TextFormat = new TextFormat();
           format.size = 14;
           _textArea.defaultTextFormat = format;
           
           var verses:Vector.<String> = new Vector.<String>(12, true);
           var lines:Vector.<String>;
           
           var days:Vector.<String> = new Vector.<String>(12, true);
           var gifts:Vector.<String> = new Vector.<String>(12, true);
           
           days[0] = 'first';
           days[1] = 'second';
           days[2] = 'third';
           days[3] = 'fourth';
           days[4] = 'fifth';
           days[5] = 'sixth';
           days[6] = 'seventh';
           days[7] = 'eighth';
           days[8] = 'ninth';
           days[9] = 'tenth';
           days[10] = 'eleventh';
           days[11] = 'twelfth';
           
           gifts[0] = 'A partridge in a pear tree';
           gifts[1] = 'Two turtle doves';
           gifts[2] = 'Three french hens';
           gifts[3] = 'Four calling birds';
           gifts[4] = 'Five golden rings';
           gifts[5] = 'Six geese a-laying';
           gifts[6] = 'Seven swans a-swimming';
           gifts[7] = 'Eight maids a-milking';
           gifts[8] = 'Nine ladies dancing';
           gifts[9] = 'Ten lords a-leaping';
           gifts[10] = 'Eleven pipers piping';
           gifts[11] = 'Twelve drummers drumming';
           
           var i:uint, j:uint, k:uint, line:String;
           
           for ( i = 0; i < 12; i++ ) {
               
               lines = new Vector.<String>(i + 2, true);
               lines[0] = "On the " + days[i] + " day of Christmas, my true love gave to me";
               
               j = i + 1;
               k = 0;
               while ( j-- > 0 )
                   lines[++k] = gifts[j];
               
               verses[i] = lines.join('\n');
               
               if ( i == 0 )
                   gifts[0] = 'And a partridge in a pear tree';
               
           }
           
           var song:String = verses.join('\n\n');
           _textArea.text = song;
           addChild(_textArea);
           
           _textArea.addEventListener(MouseEvent.MOUSE_WHEEL, _onMouseWheel);
           stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
           
       }
       
       private function _onKeyDown(e:KeyboardEvent):void {
           if ( e.keyCode == Keyboard.DOWN )
               _textArea.y -= 40;
           else if ( e.keyCode == Keyboard.UP )
               _textArea.y += 40;
       }
       
       private function _onMouseWheel(e:MouseEvent):void {
           _textArea.y += 20 * e.delta;
       }
       
   }
   

}</lang>

ALGOL 68

Works with: ALGOL 68 Genie

<lang algol68>BEGIN

 []STRING labels = ("first", "second", "third",    "fourth",
                    "fifth", "sixth",  "seventh",  "eighth",
                    "ninth", "tenth",  "eleventh", "twelfth");
 []STRING gifts = ("A partridge in a pear tree.",
                   "Two turtle doves, and",
                   "Three French hens,",
                   "Four calling birds,",
                   "Five gold rings,",
                   "Six geese a-laying,",
                   "Seven swans a-swimming,",
                   "Eight maids a-milking,",
                   "Nine ladies dancing,",
                   "Ten lords a-leaping,",
                   "Eleven pipers piping,",
                   "Twelve drummers drumming,");
 FOR day TO 12 DO
   print(("On the ", labels[day],
          " day of Christmas, my true love gave to me:", newline));
   FOR gift FROM day BY -1 TO 1 DO
     print((gifts[gift], newline))
   OD;
   print(newline)
 OD

END</lang>

Output:
On the first day of Christmas, my true love gave to me:
A partridge in a pear tree.

On the second day of Christmas, my true love gave to me:
Two turtle doves, and
A partridge in a pear tree.

On the third day of Christmas, my true love gave to me:
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the fourth day of Christmas, my true love gave to me:
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the fifth day of Christmas, my true love gave to me:
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the sixth day of Christmas, my true love gave to me:
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the seventh day of Christmas, my true love gave to me:
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the eighth day of Christmas, my true love gave to me:
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the ninth day of Christmas, my true love gave to me:
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the tenth day of Christmas, my true love gave to me:
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the eleventh day of Christmas, my true love gave to me:
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the twelfth day of Christmas, my true love gave to me:
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

AppleScript

<lang applescript>set gifts to {"A partridge in a pear tree.", "Two turtle doves, and", ¬ "Three French hens,", "Four calling birds,", ¬ "Five gold rings,", "Six geese a-laying,", ¬ "Seven swans a-swimming,", "Eight maids a-milking,", ¬ "Nine ladies dancing,", "Ten lords a-leaping,", ¬ "Eleven pipers piping,", "Twelve drummers drumming"}

set labels to {"first", "second", "third", "fourth", "fifth", "sixth", ¬ "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"}

repeat with day from 1 to 12 log "On the " & item day of labels & " day of Christmas, my true love sent to me:" repeat with gift from day to 1 by -1 log item gift of gifts end repeat log "" end repeat</lang>

Output:
On the first day of Christmas, my true love sent to me:
A partridge in a pear tree.

On the second day of Christmas, my true love sent to me:
Two turtle doves, and
A partridge in a pear tree.

On the third day of Christmas, my true love sent to me:
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the fourth day of Christmas, my true love sent to me:
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the fifth day of Christmas, my true love sent to me:
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the sixth day of Christmas, my true love sent to me:
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the seventh day of Christmas, my true love sent to me:
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the eighth day of Christmas, my true love sent to me:
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the ninth day of Christmas, my true love sent to me:
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the tenth day of Christmas, my true love sent to me:
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the eleventh day of Christmas, my true love sent to me:
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the twelfth day of Christmas, my true love sent to me:
Twelve drummers drumming
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

Ada

<lang Ada>with Ada.Text_IO; use Ada.Text_IO; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure Twelve_Days_Of_Christmas is

  type Days is (First, Second, Third, Fourth, Fifth, Sixth,

Seventh, Eighth, Ninth, Tenth, Eleventh, Twelfth);

  package E_IO is new Ada.Text_IO.Enumeration_IO(Days);
  use E_IO;
  
  Gifts : array (Days) of Unbounded_String :=
    (To_Unbounded_String(" A partridge in a pear-tree."),
     To_Unbounded_String(" Two turtle doves"),
     To_Unbounded_String(" Three French hens"),
     To_Unbounded_String(" Four calling birds"),
     To_Unbounded_String(" Five golden rings"),
     To_Unbounded_String(" Six geese a-laying"),
     To_Unbounded_String(" Seven swans a-swimming"),
     To_Unbounded_String(" Eight maids a-milking"),
     To_Unbounded_String(" Nine ladies dancing"),
     To_Unbounded_String(" Ten lords a-leaping"),
     To_Unbounded_String(" Eleven pipers piping"),
     To_Unbounded_String(" Twelve drummers drumming"));

begin

  for Day in Days loop
     Put("On the "); Put(Day, Set => Lower_Case); Put(" day of Christmas,");
     New_Line; Put_Line("My true love gave to me:");
     for D in reverse Days'First..Day loop

Put_Line(To_String(Gifts(D)));

     end loop;
     if Day = First then

Replace_Slice(Gifts(Day), 2, 2, "And a");

     end if;
     New_Line;
  end loop;

end Twelve_Days_Of_Christmas; </lang>

AutoHotkey

<lang AutoHotkey>nth := ["first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth"] lines := ["A partridge in a pear tree." ,"Two turtle doves and" ,"Three french hens" ,"Four calling birds" ,"Five golden rings" ,"Six geese a-laying" ,"Seven swans a-swimming" ,"Eight maids a-milking" ,"Nine ladies dancing" ,"Ten lords a-leaping" ,"Eleven pipers piping" ,"Twelve drummers drumming"]

full:="", mid:="" loop % lines.MaxIndex() { top:="On the " . nth[A_Index] . " day of Christmas,`nMy true love gave to me:" mid:= lines[A_Index] . "`n" . mid full:= full . top . "`n" . mid . ((A_Index<lines.MaxIndex())?"`n":"") } MsgBox % full</lang>

AWK

<lang AWK>

  1. syntax: GAWK -f THE_TWELVE_DAYS_OF_CHRISTMAS.AWK

BEGIN {

   gifts[++i] = "a partridge in a pear tree."
   gifts[++i] = "two turtle doves, and"
   gifts[++i] = "three french hens,"
   gifts[++i] = "four calling birds,"
   gifts[++i] = "five golden rings,"
   gifts[++i] = "six geese a-laying,"
   gifts[++i] = "seven swans a-swimming,"
   gifts[++i] = "eight maids a-milking,"
   gifts[++i] = "nine ladies dancing,"
   gifts[++i] = "ten lords a-leaping,"
   gifts[++i] = "eleven pipers piping,"
   gifts[++i] = "twelve drummers drumming,"
   split("first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth",days_arr," ")
   for (i=1; i<=12; i++) {
     printf("On the %s day of Christmas,\n",days_arr[i])
     print("my true love gave to me:")
     for (j=i; j>0; j--) {
       printf("%s\n",gifts[j])
     }
     print("")
   }
   exit(0)

} </lang>

Output:
On the first day of Christmas,
my true love gave to me:
a partridge in a pear tree.

On the second day of Christmas,
my true love gave to me:
two turtle doves, and
a partridge in a pear tree.

...

On the twelfth day of Christmas,
my true love gave to me:
twelve drummers drumming,
eleven pipers piping,
ten lords a-leaping,
nine ladies dancing,
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five golden rings,
four calling birds,
three french hens,
two turtle doves, and
a partridge in a pear tree.

Bracmat

<lang bracmat>( first

     second
     third
     fourth
     fifth
     sixth
     seventh
     eighth
     ninth
     tenth
     eleventh
     twelveth
 : ?days

& "A partridge in a pear tree."

     "Two turtle doves and"
     "Three french hens,"
     "Four calling birds,"
     "Five golden rings,"
     "Six geese a-laying,"
     "Seven swans a-swimming,"
     "Eight maids a-milking,"
     "Nine ladies dancing,"
     "Ten lords a-leaping,"
     "Eleven pipers piping,"
     "Twelve drummers drumming,"
 : ?gifts

& :?given & whl

 ' ( !gifts:%?gift ?gifts
   & !gift \n !given:?given
   & !days:%?day ?days
   &   out
     $ ( str
       $ ("\nOn the " !day " day of Christmas my true love gave to me:

" !given)

       )
   )

);</lang>

Befunge

This is essentially the same algorithm as Old lady swallowed a fly - just a different set of phrases and a simpler song pattern. <lang befunge>0246*+00p20#v_:#`2#g+#0:#0<>\#%"O"/#:3#:+#< g48*- >1-:!#v_\1+::"O"%\"O"/v >-#11#\0#50#< g2-:00p4v >\#%"O"/#::$#<3#$+g48*-v^\,+*+ 55!:*!!-"|":g+3<

            ^02_>#`>#< 2 5 3 1 0 \1-:#^\_^#:-1\+<00_@#:>#<$<

(On the ?|A partridge in a pear tree.||&first% andL day of Christmas,|My true l ove gave to me:2|Two turtle doves'second3|Three french hens&third4|Four calling

birds'fourth3|Five golden rings&fifth4|Six geese a-laying&sixth8|Seven swans a

-swimming(seventh7|Eight maids a-milking'eighth5|Nine ladies dancing&ninth5|Ten

lords a-leaping&tenth6|Eleven pipers piping)eleventh:|Twelve drummers drumming

(twelfth</lang>

C

<lang C> /*Abhishek Ghosh, 20th March 2014, Rotterdam*/

  1. include<stdio.h>

int main() { int i,j;

char days[12][10] =

   {
       "First",
       "Second",
       "Third",
       "Fourth",
       "Fifth",
       "Sixth",
       "Seventh",
       "Eighth",
       "Ninth",
       "Tenth",
       "Eleventh",
       "Twelfth"
   };
   
   char gifts[12][33] =
  {

"Twelve drummers drumming", "Eleven pipers piping", "Ten lords a-leaping", "Nine ladies dancing", "Eight maids a-milking", "Seven swans a-swimming", "Six geese a-laying", "Five golden rings", "Four calling birds", "Three french hens", "Two turtle doves", "And a partridge in a pear tree." };

for(i=0;i<12;i++) { printf("\n\nOn the %s day of Christmas\nMy true love gave to me:",days[i]);

for(j=i;j>=0;j--) { (i==0)?printf("\nA partridge in a pear tree."):printf("\n%s%c",gifts[11-j],(j!=0)?',':' '); } }

return 0; } </lang>

C++

<lang cpp>#include <iostream>

  1. include <array>
  2. include <string>

using namespace std;

int main() {

   const array<string, 12> days
   {
       "first",
       "second",
       "third",
       "fourth",
       "fifth",
       "sixth",
       "seventh",
       "eighth",
       "ninth",
       "tenth",
       "eleventh",
       "twelfth"
   };
 
   const array<string, 12> gifts
   {
       "And a partridge in a pear tree",
       "Two turtle doves",
       "Three french hens",
       "Four calling birds",
       "FIVE GOLDEN RINGS",
       "Six geese a-laying",
       "Seven swans a-swimming",
       "Eight maids a-milking",
       "Nine ladies dancing",
       "Ten lords a-leaping",
       "Eleven pipers piping",
       "Twelve drummers drumming"
   };
   for(int i = 0; i < days.size(); ++i)
   {
       cout << "On the " << days[i] << " day of christmas, my true love gave"
            << " to me\n";
       if(i == 0)
       {
           cout << "A partridge in a pear tree\n";
       }
       else
       {
           int j = i + 1;
           while(j-- > 0) cout << gifts[j] << "\n";
       }
       cout << "\n";
   }
   return 0;

}</lang>

C#

<lang csharp>using System;

public class TwelveDaysOfChristmas {

   public static void Main() {
       string[] days = new string[12] {
           "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
           "tenth", "eleventh", "twelfth",
       };
       string[] gifts = new string[12] {
           "A partridge in a pear tree",
           "Two turtle doves",
           "Three french hens",
           "Four calling birds",
           "Five golden rings",
           "Six geese a-laying",
           "Seven swans a-swimming",
           "Eight maids a-milking",
           "Nine ladies dancing",
           "Ten lords a-leaping",
           "Eleven pipers piping",
           "Twelve drummers drumming"
       };
       for ( int i = 0; i < 12; i++ ) {
           Console.WriteLine("On the " + days[i] + " day of Christmas, my true love gave to me");
           int j = i + 1;
           while ( j-- > 0 )
               Console.WriteLine(gifts[j]);
           Console.WriteLine();
           if ( i == 0 )
               gifts[0] = "And a partridge in a pear tree";
       }
   }

}</lang>

Clojure

Translation of: Perl 6

<lang clojure>(let [numbers '(first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth)

     gifts   ["And a partridge in a pear tree",   "Two turtle doves",     "Three French hens",      
              "Four calling birds",               "Five gold rings",      "Six geese a-laying",  
              "Seven swans a-swimming",           "Eight maids a-miling", "Nine ladies dancing",            
              "Ten lords a-leaping",              "Eleven pipers piping", "Twelve drummers drumming"]
      day     (fn [n] (printf "On the %s day of Christmas, my true love gave to me\n" (nth numbers n)))]
    (day 0)
    (println  (clojure.string/replace (first gifts) "And a" "A"))
    (dorun (for [d (range 1 12)] (do
      (println)
      (day d)
      (dorun (for [n (range d -1 -1)]
        (println (nth gifts n))))))))</lang>

COBOL

Works with: GNU Cobol version 2.0

<lang cobol> >>SOURCE FREE PROGRAM-ID. twelve-days-of-christmas.

DATA DIVISION. WORKING-STORAGE SECTION. 01 gifts-area VALUE "partridge in a pear tree "

   & "Two turtle doves              "
   & "Three french hens             "
   & "Four calling birds            "
   & "FIVE GOLDEN RINGS             "
   & "Six geese a-laying            "
   & "Seven swans a-swimming        "
   & "Eight maids a-milking         "
   & "Nine ladies dancing           "
   & "Ten lords a-leaping           "
   & "Eleven pipers piping          "
   & "Twelve drummers drumming      ".
   03  gifts                           PIC X(30) OCCURS 12 TIMES
                                       INDEXED BY gift-idx.

01 ordinals-area VALUE "first second third fourth fifth "

   & "sixth     seventh   eighth    ninth     tenth     eleventh  twelfth   ".
   03  ordinals                        PIC X(10) OCCURS 12 TIMES.

01 day-num PIC 99 COMP.

PROCEDURE DIVISION.

   PERFORM VARYING day-num FROM 1 BY 1 UNTIL day-num > 12
       DISPLAY "On the " FUNCTION TRIM(ordinals (day-num)) " day of Christmas,"
           " my true love gave to me"
       IF day-num = 1
           DISPLAY "A " gifts (1)
       ELSE
           PERFORM VARYING gift-idx FROM day-num BY -1 UNTIL gift-idx = 1
               DISPLAY gifts (gift-idx)
           END-PERFORM
           DISPLAY "And a " gifts (1)
       END-IF
       
       DISPLAY SPACE
   END-PERFORM
   .

END PROGRAM twelve-days-of-christmas.</lang>

Common Lisp

<lang lisp>(let

  ((names '(first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth))
   (gifts '( "A partridge in a pear tree." "Two turtle doves and"    "Three French hens,"
             "Four calling birds,"         "Five gold rings,"        "Six geese a-laying,"
             "Seven swans a-swimming,"     "Eight maids a-milking,"  "Nine ladies dancing,"
             "Ten lords a-leaping,"        "Eleven pipers piping,"   "Twelve drummers drumming," )))
  (loop for day in names for i from 0 doing
    (format t "On the ~a day of Christmas, my true love gave to me:" (string-downcase day))
    (loop for g from i downto 0 doing (format t "~a~%" (nth g gifts)))
    (format t "~%~%")))</lang>
Output:
On the first day of Christmas, my true love gave to me:
A partridge in a pear tree.

On the second day of Christmas, my true love gave to me:
Two turtle doves and
A partridge in a pear tree.

On the third day of Christmas, my true love gave to me:
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the fourth day of Christmas, my true love gave to me:
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the fifth day of Christmas, my true love gave to me:
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the sixth day of Christmas, my true love gave to me:
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the seventh day of Christmas, my true love gave to me:
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the eighth day of Christmas, my true love gave to me:
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the ninth day of Christmas, my true love gave to me:
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the tenth day of Christmas, my true love gave to me:
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the eleventh day of Christmas, my true love gave to me:
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the twelfth day of Christmas, my true love gave to me:
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

D

Translation of: Python

<lang d>immutable gifts = "A partridge in a pear tree. Two turtle doves Three french hens Four calling birds Five golden rings Six geese a-laying Seven swans a-swimming Eight maids a-milking Nine ladies dancing Ten lords a-leaping Eleven pipers piping Twelve drummers drumming";

immutable days = "first second third fourth fifth

                 sixth seventh eighth ninth tenth
                 eleventh twelfth";

void main() @safe {

   import std.stdio, std.string, std.range;
   foreach (immutable n, immutable day; days.split) {
       auto g = gifts.splitLines.take(n + 1).retro;
       writeln("On the ", day,
               " day of Christmas\nMy true love gave to me:\n",
               g[0 .. $ - 1].join('\n'),
               (n > 0 ? " and\n" ~ g.back : g.back.capitalize), '\n');
   }

}</lang>

dc

<lang dc>0

d [first] r :n d [A partridge in a pear tree.] r :g 1 +

d [second] r :n d [Two turtle doves and] r :g 1 +

d [third] r :n d [Three French hens,] r :g 1 +

d [fourth] r :n d [Four calling birds,] r :g 1 +

d [fifth] r :n d [Five gold rings,] r :g 1 +

d [sixth] r :n d [Six geese a-laying,] r :g 1 +

d [seventh] r :n d [Seven swans a-swimming,] r :g 1 +

d [eighth] r :n d [Eight maids a-milking,] r :g 1 +

d [ninth] r :n d [Nine ladies dancing,] r :g 1 +

d [tenth] r :n d [Ten lords a-leaping,] r :g 1 +

d [eleventh] r :n d [Eleven pipers piping,] r :g 1 +

d [twelfth] r :n

 [Twelve drummers drumming,]   r :g 

[

 d 
 ;g n 
 10 P 

] sp

[

 d 
 0 r !<p 
 1 - 
 d 
 0 r !<r 

] sr

[

 [On the ] n 
 d ;n n 
 [ day of Christmas, my true love gave to me:] n 
 10 P 
 d 
 lr x s_
 10 P
 1 + 
 d 
 12 r <l

] sl

0 ll x </lang>

Output:
On the first day of Christmas, my true love gave to me:
A partridge in a pear tree.

On the second day of Christmas, my true love gave to me:
Two turtle doves and
A partridge in a pear tree.

On the third day of Christmas, my true love gave to me:
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the fourth day of Christmas, my true love gave to me:
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the fifth day of Christmas, my true love gave to me:
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the sixth day of Christmas, my true love gave to me:
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the seventh day of Christmas, my true love gave to me:
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the eighth day of Christmas, my true love gave to me:
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the ninth day of Christmas, my true love gave to me:
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the tenth day of Christmas, my true love gave to me:
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the eleventh day of Christmas, my true love gave to me:
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the twelfth day of Christmas, my true love gave to me:
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

Eiffel

<lang Eiffel> class APPLICATION

create make

feature

make do twelve_days_of_christmas end

feature {NONE}

twelve_days_of_christmas -- Christmas carol: Twelve days of christmas. local i, j: INTEGER do create gifts.make_empty create days.make_empty gifts := <<"A partridge in a pear tree.", "Two turtle doves and", "Three french hens", "Four calling birds", "Five golden rings", "Six geese a-laying", "Seven swans a-swimming", "Eight maids a-milking", "Nine ladies dancing", "Ten lords a-leaping", "Eleven pipers piping", "Twelve drummers drumming", "And a partridge in a pear tree.", "Two turtle doves">> days := <<"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "Twelfth">> from i := 1 until i > days.count loop io.put_string ("On the " + days [i] + " day of Christmas.%N") io.put_string ("My true love gave to me:%N") from j := i until j <= 0 loop if i = 12 and j = 2 then io.put_string (gifts [14] + "%N") io.put_string (gifts [13] + "%N") j := j - 1 else io.put_string (gifts [j] + "%N") end j := j - 1 end io.new_line i := i + 1 end end

gifts: ARRAY [STRING]

days: ARRAY [STRING]

end </lang>

Elixir

<lang elixir>gifts = """ A partridge in a pear tree Two turtle doves and Three french hens Four calling birds Five golden rings Six geese a-laying Seven swans a-swimming Eight maids a-milking Nine ladies dancing Ten lords a-leaping Eleven pipers piping Twelve drummers drumming """ |> String.split("\n", trim: true)

days = ~w(first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth)

Enum.with_index(days) |> Enum.each(fn {day, i} ->

 IO.puts "On the #{day} day of Christmas"
 IO.puts "My true love gave to me:"
 Enum.take(gifts, i+1) |> Enum.reverse |> Enum.each(&IO.puts &1)
 IO.puts ""

end)</lang>

Output:
On the first day of Christmas
My true love gave to me:
A partridge in a pear tree

On the second day of Christmas
My true love gave to me:
Two turtle doves and
A partridge in a pear tree

...

On the twelfth day of Christmas
My true love gave to me:
Twelve drummers drumming
Eleven pipers piping
Ten lords a-leaping
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

Erlang

<lang erlang>-module(twelve_days). -export([gifts_for_day/1]).

names(N) -> lists:nth(N,

             ["first",   "second", "third", "fourth", "fifth",    "sixth", 
              "seventh", "eighth", "ninth", "tenth",  "eleventh", "twelfth" ]).

gifts() -> [ "A partridge in a pear tree.", "Two turtle doves and",

            "Three French hens,",          "Four calling birds,",
            "Five gold rings,",            "Six geese a-laying,",
            "Seven swans a-swimming,",     "Eight maids a-milking,",
            "Nine ladies dancing,",        "Ten lords a-leaping,",
            "Eleven pipers piping,",       "Twelve drummers drumming," ].

gifts_for_day(N) ->

 "On the " ++ names(N) ++ " day of Christmas, my true love sent to me:\n" ++
 string:join(lists:reverse(lists:sublist(gifts(), N)), "\n").

main(_) -> lists:map(fun(N) -> io:fwrite("~s~n~n", [gifts_for_day(N)]) end,

                    lists:seq(1,12)).

</lang>

Output:
On the first day of Christmas, my true love sent to me:

A partridge in a pear tree.

On the second day of Christmas, my true love sent to me: Two turtle doves and A partridge in a pear tree.

[...]

On the twelfth day of Christmas, my true love sent to me: Twelve drummers drumming, Eleven pipers piping, Ten lords a-leaping, Nine ladies dancing, Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five gold rings, Four calling birds, Three French hens, Two turtle doves and A partridge in a pear tree.</.pre>


=={{header|F Sharp|F#}}== <lang fsharp>let gifts = [

   "And a partridge in a pear tree";
   "Two turtle doves";
   "Three french hens";
   "Four calling birds";
   "FIVE GOLDEN RINGS";
   "Six geese a-laying";
   "Seven swans a-swimming";
   "Eight maids a-milking";
   "Nine ladies dancing";
   "Ten lords a-leaping";
   "Eleven pipers piping";
   "Twelve drummers drumming"

]

let days = [

   "first"; "second"; "third"; "fourth"; "fifth"; "sixth"; "seventh"; "eighth";
   "ninth"; "tenth"; "eleventh"; "twelfth"

]

let displayGifts day =

   printfn "On the %s day of Christmas, my true love gave to me" days.[day]
   if day = 0 then
       printfn "A partridge in a pear tree"
   else
       List.iter (fun i -> printfn "%s" gifts.[i]) [day..(-1)..0]
   printf "\n"
   

List.iter displayGifts [0..11]</lang>

=={{header|Forth}}== {{works with|GNU Forth}} <lang forth>create ordinals s" first" 2, s" second" 2, s" third" 2, s" fourth" 2,

               s" fifth" 2, s" sixth"  2, s" seventh"  2, s" eighth" 2,
               s" ninth" 2, s" tenth"  2, s" eleventh" 2, s" twelfth" 2,
ordinal ordinals swap 2 * cells + 2@ type ;

create gifts s" A partridge in a pear tree." 2,

            s" Two turtle doves and" 2,
            s" Three French hens," 2,
            s" Four calling birds," 2,
            s" Five gold rings," 2,
            s" Six geese a-laying," 2,
            s" Seven swans a-swimming," 2,
            s" Eight maids a-milking," 2,
            s" Nine ladies dancing," 2,
            s" Ten lords a-leaping," 2,
            s" Eleven pipers piping," 2,
            s" Twelve drummers drumming," 2,
gift gifts swap 2 * cells + 2@ type cr ;
day
 s" On the " type 
 dup ordinal 
 s"  day of Christmas, my true love gave to me:" type
 cr
 -1 swap -do
   i gift
 1 -loop
 cr
 ;
main
 12 0 do i day loop

main bye</lang>

{{Out}} <pre>On the first day of Christmas, my true love gave to me: A partridge in a pear tree.

On the second day of Christmas, my true love gave to me: Two turtle doves and A partridge in a pear tree.

On the third day of Christmas, my true love gave to me: Three French hens, Two turtle doves and A partridge in a pear tree.

On the fourth day of Christmas, my true love gave to me: Four calling birds, Three French hens, Two turtle doves and A partridge in a pear tree.

On the fifth day of Christmas, my true love gave to me: Five gold rings, Four calling birds, Three French hens, Two turtle doves and A partridge in a pear tree.

On the sixth day of Christmas, my true love gave to me: Six geese a-laying, Five gold rings, Four calling birds, Three French hens, Two turtle doves and A partridge in a pear tree.

On the seventh day of Christmas, my true love gave to me: Seven swans a-swimming, Six geese a-laying, Five gold rings, Four calling birds, Three French hens, Two turtle doves and A partridge in a pear tree.

On the eighth day of Christmas, my true love gave to me: Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five gold rings, Four calling birds, Three French hens, Two turtle doves and A partridge in a pear tree.

On the ninth day of Christmas, my true love gave to me: Nine ladies dancing, Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five gold rings, Four calling birds, Three French hens, Two turtle doves and A partridge in a pear tree.

On the tenth day of Christmas, my true love gave to me: Ten lords a-leaping, Nine ladies dancing, Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five gold rings, Four calling birds, Three French hens, Two turtle doves and A partridge in a pear tree.

On the eleventh day of Christmas, my true love gave to me: Eleven pipers piping, Ten lords a-leaping, Nine ladies dancing, Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five gold rings, Four calling birds, Three French hens, Two turtle doves and A partridge in a pear tree.

On the twelfth day of Christmas, my true love gave to me: Twelve drummers drumming, Eleven pipers piping, Ten lords a-leaping, Nine ladies dancing, Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five gold rings, Four calling birds, Three French hens, Two turtle doves and

A partridge in a pear tree.

Go

<lang go>package main

import ( "fmt" "strings" )

func main() { numbers := []string{"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"}

gifts := []string{"And a partridge in a pear tree", "Two turtle doves", "Three French hens", "Four calling birds", "Five gold rings", "Six geese a-laying", "Seven swans a-swimming", "Eight maids a-miling", "Nine ladies dancing", "Ten lords a-leaping", "Eleven pipers piping", "Twelve drummers drumming"}

day := func(n int) { fmt.Printf("On the %s day of Christams, my true love gave to me\n", numbers[n]) }

day(0) fmt.Println(strings.Replace(gifts[0], "And a", "A", 1)) for d := 1; d < 12; d++ { fmt.Println() day(d) for g := d; g >= 0; g-- { fmt.Println(gifts[g]) } } }</lang>

Groovy

<lang groovy>def presents = ['A partridge in a pear tree.', 'Two turtle doves', 'Three french hens', 'Four calling birds',

       'Five golden rings', 'Six geese a-laying', 'Seven swans a-swimming', 'Eight maids a-milking',
       'Nine ladies dancing', 'Ten lords a-leaping', 'Eleven pipers piping', 'Twelve drummers drumming']

['first', 'second', 'third', 'forth', 'fifth', 'sixth', 'seventh', 'eight', 'ninth', 'tenth', 'eleventh', 'Twelfth'].eachWithIndex{ day, dayIndex ->

   println "On the $day day of Christmas"
   println 'My true love gave to me:'
   (dayIndex..0).each { p ->
       print presents[p]
       println p == 1 ? ' and' : 
   }
   println()

}</lang>

Haskell

Translation of: F#

<lang haskell>gifts :: [String] gifts = [

   "And a partridge in a pear tree!",
   "Two turtle doves,",
   "Three french hens,",
   "Four calling birds,",
   "FIVE GOLDEN RINGS,",
   "Six geese a-laying,",
   "Seven swans a-swimming,",
   "Eight maids a-milking,",
   "Nine ladies dancing,",
   "Ten lords a-leaping,",
   "Eleven pipers piping,",
   "Twelve drummers drumming," ]

days :: [String] days = [

   "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth",
   "ninth", "tenth", "eleventh", "twelfth" ]

verseOfTheDay :: Int -> IO () verseOfTheDay day = do

   putStrLn $ "On the " ++ days !! day ++ " day of Christmas my true love gave to me... " 
   mapM_ putStrLn [dayGift day d | d <- [day, day-1..0]] 
   putStrLn ""
   where dayGift 0 _ = "A partridge in a pear tree!"
         dayGift _ gift = gifts !! gift

main :: IO () main = mapM_ verseOfTheDay [0..11] </lang>

Output:
On the first day of Christmas my true love gave to me... 
A partridge in a pear tree!

On the second day of Christmas my true love gave to me... 
Two turtle doves,
And a partridge in a pear tree!

On the third day of Christmas my true love gave to me... 
Three french hens,
Two turtle doves,
And a partridge in a pear tree!

On the fourth day of Christmas my true love gave to me... 
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree!

On the fifth day of Christmas my true love gave to me... 
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree!

On the sixth day of Christmas my true love gave to me... 
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree!

On the seventh day of Christmas my true love gave to me... 
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree!

On the eighth day of Christmas my true love gave to me... 
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree!

On the ninth day of Christmas my true love gave to me... 
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree!

On the tenth day of Christmas my true love gave to me... 
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree!

On the eleventh day of Christmas my true love gave to me... 
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree!

On the twelfth day of Christmas my true love gave to me... 
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree!

Icon and Unicon

Works in both languages. <lang unicon>procedure main()

   days := ["first","second","third","fourth","fifth","sixth","seventh",
            "eighth","ninth","tenth","eleventh","twelveth"]
   gifts := ["A partridge in a pear tree.", "Two turtle doves and",
             "Three french hens,", "Four calling birds,",
             "Five golden rings,", "Six geese a-laying,",
             "Seven swans a-swimming,", "Eight maids a-milking,",
             "Nine ladies dancing,", "Ten lords a-leaping,",
             "Eleven pipers piping,", "Twelve drummers drumming,"]
   every write("\nOn the ",days[day := 1 to 12]," day of Christmas my true love gave to me:") do
       every write(" ",gifts[day to 1 by -1])

end</lang>

J

<lang j>require 'strings' NB. not necessary for versions > j6

days=: ;:'first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth'

gifts=: <;.2 ] 0 : 0 And a partridge in a pear tree. Two turtle doves, Three french hens, Four calling birds, Five golden rings, Six geese a-laying, Seven swans a-swimming, Eight maids a-milking, Nine ladies dancing, Ten lords a-leaping, Eleven pipers piping, Twelve drummers drumming, )

firstline=: 'On the ' , ,&(' day of Christmas, my true love gave to me',LF)

chgFirstVerse=: rplc&('nd a';)&.>@{. , }.

makeVerses=: [: chgFirstVerse (firstline&.> days) ,&.> [: <@;@|.\ gifts"_

singCarol=: LF joinstring makeVerses</lang>

Java

<lang java>public class TwelveDaysOfChristmas {

   final static String[] gifts = {
       "A partridge in a pear tree.", "Two turtle doves and",
       "Three french hens", "Four calling birds",
       "Five golden rings", "Six geese a-laying",
       "Seven swans a-swimming", "Eight maids a-milking",
       "Nine ladies dancing", "Ten lords a-leaping",
       "Eleven pipers piping", "Twelve drummers drumming",
       "And a partridge in a pear tree.", "Two turtle doves"
   };
   final static String[] days = {
       "first", "second", "third", "fourth", "fifth", "sixth", "seventh",
       "eighth", "ninth", "tenth", "eleventh", "Twelfth"
   };
   public static void main(String[] args) {
       for (int i = 0; i < days.length; i++) {
           System.out.printf("%nOn the %s day of Christmas%n", days[i]);
           System.out.println("My true love gave to me:");
           for (int j = i; j >= 0; j--)
               System.out.println(gifts[i == 11 && j < 2 ? j + 12 : j]);
       }
   }

}</lang>

JavaScript

<lang JavaScript> var days = [

   'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth',
   'tenth', 'eleventh', 'twelfth',

];

var gifts = [

   "A partridge in a pear tree",
   "Two turtle doves",
   "Three french hens",
   "Four calling birds",
   "Five golden rings",
   "Six geese a-laying",
   "Seven swans a-swimming",
   "Eight maids a-milking",
   "Nine ladies dancing",
   "Ten lords a-leaping",
   "Eleven pipers piping",
   "Twelve drummers drumming"

];

var lines, verses = [], song;

for ( var i = 0; i < 12; i++ ) {

   lines = [];
   lines[0] = "On the " + days[i] + " day of Christmas, my true love gave to me";
   
   var j = i + 1;
   var k = 0;
   while ( j-- > 0 )
       lines[++k] = gifts[j];


   verses[i] = lines.join('\n');
   
   if ( i == 0 )
       gifts[0] = "And a partridge in a pear tree";
   

}

song = verses.join('\n\n'); document.write(song); </lang>


Alternatively, in a functional style of JavaScript, we can define the ancient song "strPrepn the lstOrdinal[i] strUnit of strHoliday" as an expression, and return that expression in a human-legible and machine-parseable JSON string translation, for further analysis and processing :-)

<lang JavaScript>JSON.stringify(

 (function (
   strPrepn,
   strHoliday,
   strUnit,
   strRole,
   strProcess,
   strRecipient
 ) {
   var lstOrdinal =
     'first second third fourth fifth sixth\
          seventh eighth ninth tenth eleventh twelfth'
     .split(/\s+/),
     lngUnits = lstOrdinal.length,
     lstGoods =
     'A partridge in a pear tree.\
          Two turtle doves\
          Three french hens\
          Four calling birds\
          Five golden rings\
          Six geese a-laying\
          Seven swans a-swimming\
          Eight maids a-milking\
          Nine ladies dancing\
          Ten lords a-leaping\
          Eleven pipers piping\
          Twelve drummers drumming'
     .split(/\s{2,}/),
     lstReversed = (function () {
       var lst = lstGoods.slice(0);
       return (lst.reverse(), lst);
     })(),
     strProvenance = [strRole, strProcess, strRecipient + ':'].join(' '),
     strPenultimate = lstReversed[lngUnits - 2] + ' and',
     strFinal = lstGoods[0];
   return lstOrdinal.reduce(
     function (sofar, day, i) {
       return sofar.concat(
         [
           [
             [ // abstraction of line 1
               strPrepn,
               'the',
               lstOrdinal[i],
               strUnit,
               'of',
               strHoliday
             ].join(' '),
             strProvenance
           ].concat( // reversed descent through memory
             (i > 1 ? [lstGoods[i]] : []).concat(
               lstReversed.slice(
                 lngUnits - i,
                 lngUnits - 2
               )
             ).concat( // penultimate line ends with 'and'
               [
                 strPenultimate,
                 strFinal
               ].slice(i ? 0 : 1)
             )
           )
         ]
       );
     }, []
   );
 })(
   'On', 'Christmas', 'day', 'my true love', 'gave to', 'me'
 ), null, 2

);</lang>

Note that the Google Closure compiler's translation of this would be half the size, but rather less legible. (It does make interesting suggestions though – the semi-colon segmentation of the verses below is a trick that might be worth remembering).

<lang JavaScript>JSON.stringify(function (h, k, l, f, m, n) {

 var c =
   "first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth"
   .split(" "),
   d = c.length,
   e =
   "A partridge in a pear tree.;Two turtle doves;Three french hens;Four calling birds;Five golden rings;Six geese a-laying;Seven swans a-swimming;Eight maids a-milking;Nine ladies dancing;Ten lords a-leaping;Eleven pipers piping;Twelve drummers drumming"
   .split(";"),
   g = function () {
     var b = e.slice(0);
     return b.reverse(), b;
   }(),
   p = [f, m, n + ":"].join(" "),
   q = g[d - 2] + " and",
   r = e[0];
   
 return c.reduce(function (b, f, a) {
   return b.concat([[[h, "the", c[a], l, "of", k].join(" "), p].concat((1 <
     a ? [e[a]] : []).concat(g.slice(d - a, d - 2)).concat([q, r].slice(a ?
     0 : 1)))]);
 }, []);

}("On", "Christmas", "day", "my true love", "gave to", "me"), null, 2);</lang>

Formatted JSON output (the expanded and Closure-compiled versions above both yield the same output).

<lang JavaScript>[

 [
   "On the first day of Christmas",
   "my true love gave to me:",
   "A partridge in a pear tree."
 ],
 [
   "On the second day of Christmas",
   "my true love gave to me:",
   "Two turtle doves and",
   "A partridge in a pear tree."
 ],
 [
   "On the third day of Christmas",
   "my true love gave to me:",
   "Three french hens",
   "Two turtle doves and",
   "A partridge in a pear tree."
 ],
 [
   "On the fourth day of Christmas",
   "my true love gave to me:",
   "Four calling birds",
   "Three french hens",
   "Two turtle doves and",
   "A partridge in a pear tree."
 ],
 [
   "On the fifth day of Christmas",
   "my true love gave to me:",
   "Five golden rings",
   "Four calling birds",
   "Three french hens",
   "Two turtle doves and",
   "A partridge in a pear tree."
 ]

//... etc.

]</lang>

Kotlin

Translation of: Java

<lang scala>fun main(x: Array<String>) {

   val gifts = listOf(
       "A partridge in a pear tree.", "Two turtle doves and",
       "Three french hens", "Four calling birds",
       "Five golden rings", "Six geese a-laying",
       "Seven swans a-swimming", "Eight maids a-milking",
       "Nine ladies dancing", "Ten lords a-leaping",
       "Eleven pipers piping", "Twelve drummers drumming",
       "And a partridge in a pear tree.", "Two turtle doves")

   val days = listOf("first", "second", "third", "fourth", "fifth", "sixth", "seventh",
       "eighth", "ninth", "tenth", "eleventh", "Twelfth")

       for (i in days.indices) {
           println("On the ${days[i]} day of Christmas")
           println("My true love gave to me:")
           for (j in i downTo 0)
               println(gifts[if (i == 11 && j < 2) j + 12 else j])
       }

}</lang>

<lang logo>make "numbers [first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth] make "gifts [[And a partridge in a pear tree] [Two turtle doves] [Three French hens]

              [Four calling birds]             [Five gold rings]      [Six geese a-laying]
              [Seven swans a-swimming]         [Eight maids a-miling] [Nine ladies dancing]
              [Ten lords a-leaping]            [Eleven pipers piping] [Twelve drummers drumming]]

to nth :n

 print (sentence [On the] (item :n :numbers) [day of Christmas, my true love gave to me])

end

nth 1 print [A partridge in a pear tree]

for [d 2 12] [

 print []
 nth :d
 for [g :d 1] [
   print item :g gifts
 ]

] bye</lang>

LOLCODE

Works with: LCI 0.10

<lang>CAN HAS STDIO? HAI 1.2

I HAS A Dayz ITZ A BUKKIT Dayz HAS A SRS 1 ITZ "first" Dayz HAS A SRS 2 ITZ "second" Dayz HAS A SRS 3 ITZ "third" Dayz HAS A SRS 4 ITZ "fourth" Dayz HAS A SRS 5 ITZ "fifth" Dayz HAS A SRS 6 ITZ "sixth" Dayz HAS A SRS 7 ITZ "seventh" Dayz HAS A SRS 8 ITZ "eighth" Dayz HAS A SRS 9 ITZ "ninth" Dayz HAS A SRS 10 ITZ "tenth" Dayz HAS A SRS 11 ITZ "eleventh" Dayz HAS A SRS 12 ITZ "twelfth"

I HAS A Prezents ITZ A BUKKIT Prezents HAS A SRS 1 ITZ "A partridge in a pear tree" Prezents HAS A SRS 2 ITZ "Two turtle doves" Prezents HAS A SRS 3 ITZ "Three French hens" Prezents HAS A SRS 4 ITZ "Four calling birds" Prezents HAS A SRS 5 ITZ "Five gold rings" Prezents HAS A SRS 6 ITZ "Six geese a-laying" Prezents HAS A SRS 7 ITZ "Seven swans a-swimming" Prezents HAS A SRS 8 ITZ "Eight maids a-milking" Prezents HAS A SRS 9 ITZ "Nine ladies dancing" Prezents HAS A SRS 10 ITZ "Ten lords a-leaping" Prezents HAS A SRS 11 ITZ "Eleven pipers piping" Prezents HAS A SRS 12 ITZ "Twelve drummers drumming"

IM IN YR Outer UPPIN YR i WILE DIFFRINT i AN 12

 I HAS A Day ITZ SUM OF i AN 1
 VISIBLE "On the " !
 VISIBLE Dayz'Z SRS Day !
 VISIBLE " day of Christmas, my true love gave to me"
 IM IN YR Inner UPPIN YR j WILE DIFFRINT j AN  Day
   I HAS A Count ITZ DIFFERENCE OF Day AN j
   VISIBLE Prezents'Z SRS Count
 IM OUTTA YR Inner
 BOTH SAEM i AN 0 
 O RLY? 
   YA RLY
     Prezents'Z SRS 1 R "And a partridge in a pear tree"
 OIC
 DIFFRINT i AN 11
   O RLY?
     YA RLY
       VISIBLE ""
 OIC

IM OUTTA YR Outer

KTHXBYE</lang>

Lua

<lang Lua> local days = {

   'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth',
   'tenth', 'eleventh', 'twelfth',

}

local gifts = {

   "A partridge in a pear tree",
   "Two turtle doves",
   "Three french hens",
   "Four calling birds",
   "Five golden rings",
   "Six geese a-laying",
   "Seven swans a-swimming",
   "Eight maids a-milking",
   "Nine ladies dancing",
   "Ten lords a-leaping",
   "Eleven pipers piping",
   "Twelve drummers drumming",

}

local verses = {}

for i = 1, 12 do

   local lines = {}
   lines[1] = "On the " .. days[i] .. " day of Christmas, my true love gave to me"
   
   local j = i
   local k = 2
   repeat
       lines[k] = gifts[j]
       k = k + 1
       j = j - 1
   until j == 0
   
   verses[i] = table.concat(lines, '\n')

end

print(table.concat(verses, '\n\n')) </lang>


Mathematica

<lang Mathematica> daysarray = {"first", "second", "third", "fourth", "fifth", "sixth",

  "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"};

giftsarray = {"And a partridge in a pear tree.", "Two turtle doves,",

  "Three french hens,", "Four calling birds,", "FIVE GOLDEN RINGS,", 
  "Six geese a-laying,", "Seven swans a-swimming,", 
  "Eight maids a-milking,", "Nine ladies dancing,", 
  "Ten lords a-leaping,", "Eleven pipers piping,", 
  "Twelve drummers drumming,"};

Do[

Print["On the " <> daysarrayi <> 
   " day of Christmas, my true love gave to me: "]
 Do [
  Print[If[i == 1, "A partridge in a pear tree.", giftsarrayj]]
  , {j, i, 1, -1}]
 Print[]
, {i, 1, 12}]

</lang>

Output:
On the first day of Christmas, my true love gave to me: 
A partridge in a pear tree.

On the second day of Christmas, my true love gave to me: 
Two turtle doves,
And a partridge in a pear tree.

On the third day of Christmas, my true love gave to me: 
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the fourth day of Christmas, my true love gave to me: 
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the fifth day of Christmas, my true love gave to me: 
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the sixth day of Christmas, my true love gave to me: 
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the seventh day of Christmas, my true love gave to me: 
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the eighth day of Christmas, my true love gave to me: 
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the ninth day of Christmas, my true love gave to me: 
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the tenth day of Christmas, my true love gave to me: 
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the eleventh day of Christmas, my true love gave to me: 
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the twelfth day of Christmas, my true love gave to me: 
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GOLDEN RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

Nim

Translation of: Python

<lang nim>import strutils, algorithm

const

 gifts = """A partridge in a pear tree.

Two turtle doves Three french hens Four calling birds Five golden rings Six geese a-laying Seven swans a-swimming Eight maids a-milking Nine ladies dancing Ten lords a-leaping Eleven pipers piping Twelve drummers drumming""".splitLines()

 days = "first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth".split(' ')

for n, day in days:

 var g = (gifts[0..n])
 reverse(g)
 echo "\nOn the ", day, " day of Christmas\nMy true love gave to me:\n" &
   g[0 .. -2].join("\n") &
   (if n > 0: " and\n" & g[g.high] else: capitalize(g[g.high]))</lang>

Objeck

Translation of: C sharp – C#

<lang objeck> class TwelveDaysOfChristmas {

 function : Main(args : String[]) ~ Nil {
   days := ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
           "tenth", "eleventh", "twelfth"];

       gifts := ["A partridge in a pear tree",
           "Two turtle doves",
           "Three french hens",
           "Four calling birds",
           "Five golden rings",
           "Six geese a-laying",
           "Seven swans a-swimming",
           "Eight maids a-milking",
           "Nine ladies dancing",
           "Ten lords a-leaping",
           "Eleven pipers piping",
           "Twelve drummers drumming"];

       for(i := 0; i < days->Size(); i+=1;) {
         IO.Console->Print("On the ")->Print(days[i])->PrintLine(" day of Christmas, my true love gave to me");
         j := i + 1;
         while(j > 0 ) {
           j -= 1;
           gifts[j]->PrintLine();
         };
     
          IO.Console->PrintLine();

          if (i = 0) {
            gifts[0] := "And a partridge in a pear tree";
          };
       };
   }

} </lang>

PARI/GP

<lang parigp>days=["first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth"]; gifts=["And a partridge in a pear tree.", "Two turtle doves", "Three french hens", "Four calling birds", "Five golden rings", "Six geese a-laying", "Seven swans a-swimming", "Eight maids a-milking", "Nine ladies dancing", "Ten lords a-leaping", "Eleven pipers piping", "Twelve drummers drumming"]; { for(i=1,#days,

 print("On the "days[i]" day of Christmas, my true love gave to me:");
 forstep(j=i,2,-1,print("\t"gifts[j]", "));
 print(if(i==1,"\tA partridge in a pear tree.",Str("\t",gifts[1])))

) }</lang>

Output:
On the first day of Christmas, my true love gave to me:
        A partridge in a pear tree.
On the second day of Christmas, my true love gave to me:
        Two turtle doves,
        And a partridge in a pear tree.
[...]
On the twelfth day of Christmas, my true love gave to me:
        Twelve drummers drumming,
        Eleven pipers piping,
        Ten lords a-leaping,
        Nine ladies dancing,
        Eight maids a-milking,
        Seven swans a-swimming,
        Six geese a-laying,
        Five golden rings,
        Four calling birds,
        Three french hens,
        Two turtle doves,
        And a partridge in a pear tree.

Pascal

This should work with any modern Pascal implementation that has a string type, e.g. Free Pascal.

<lang pascal>program twelve_days(output);

const

 days:  array[1..12] of string =
   ( 'first',   'second', 'third', 'fourth', 'fifth',    'sixth',
     'seventh', 'eighth', 'ninth', 'tenth',  'eleventh', 'twelfth' );
 gifts: array[1..12] of string =
   ( 'A partridge in a pear tree.',
     'Two turtle doves and',
     'Three French hens,',
     'Four calling birds,',
     'Five gold rings,',
     'Six geese a-laying,',
     'Seven swans a-swimming,',
     'Eight maids a-milking,',
     'Nine ladies dancing,',
     'Ten lords a-leaping,',
     'Eleven pipers piping,',
     'Twelve drummers drumming,' );

var

  day, gift: integer;

begin

  for day := 1 to 12 do begin
    writeln('On the ', days[day], ' day of Christmas, my true love gave to me:');
    for gift := day downto 1 do
      writeln(gifts[gift]);
    writeln
  end

end.</lang>

Output:
On the first day of Christmas, my true love gave to me:
A partridge in a pear tree.

On the second day of Christmas, my true love gave to me:
Two turtle doves and
A partridge in a pear tree.

[...]

On the twelfth day of Christmas, my true love gave to me:
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

Here's a version that works in ISO Standard Pascal, albeit with extraneous spaces in the output:

<lang pascal>program twelve_days_iso(output);

const

 days:  array[1..12, 1..8] of char =
   ( 'first   ', 'second  ', 'third   ', 'fourth  ',
     'fifth   ', 'sixth   ', 'seventh ', 'eighth  ',
     'ninth   ', 'tenth   ', 'eleventh', 'twelfth ' );
 gifts: array[1..12, 1..27] of char =
   ( 'A partridge in a pear tree.',
     'Two turtle doves and       ',
     'Three French hens,         ',
     'Four calling birds,        ',
     'Five gold rings,           ',
     'Six geese a-laying,        ',
     'Seven swans a-swimming,    ',
     'Eight maids a-milking,     ',
     'Nine ladies dancing,       ',
     'Ten lords a-leaping,       ',
     'Eleven pipers piping,      ',
     'Twelve drummers drumming,  ' );

var

  day, gift: integer;

begin

  for day := 1 to 12 do begin
    writeln('On the ', days[day], ' day of Christmas, my true love gave to me:');
    for gift := day downto 1 do
      writeln(gifts[gift]);
    writeln
  end

end.</lang>

Output:
On the first    day of Christmas, my true love gave to me:
A partridge in a pear tree.

On the second   day of Christmas, my true love gave to me:
Two turtle doves and       
A partridge in a pear tree.

[...]

On the twelfth  day of Christmas, my true love gave to me:
Twelve drummers drumming,  
Eleven pipers piping,      
Ten lords a-leaping,       
Nine ladies dancing,       
Eight maids a-milking,     
Seven swans a-swimming,    
Six geese a-laying,        
Five gold rings,           
Four calling birds,        
Three French hens,         
Two turtle doves and       
A partridge in a pear tree.

Perl

<lang perl>use v5.10;

my @days = qw{ first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth };

chomp ( my @gifts = grep { /\S/ } );

while ( my $day = shift @days ) {

   say "On the $day day of Christmas,\nMy true love gave to me:";
   say for map { $day eq 'first' ? s/And a/A/r : $_ } @gifts[@days .. @gifts-1];
   say "";

}

__DATA__ Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying Five golden rings Four calling birds Three french hens Two turtle doves And a partridge in a pear tree.</lang>

Output:
On the first day of Christmas,
My true love gave to me:
A partridge in a pear tree.

On the second day of Christmas,
My true love gave to me:
Two turtle doves
And a partridge in a pear tree.

...

On the twelfth day of Christmas,
My true love gave to me:
Twelve drummers drumming
Eleven pipers piping
Ten lords a-leaping
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves
And a partridge in a pear tree.

Perl 6

<lang perl6>my @days = <first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth>;

my @gifts = lines q:to/END/;

 And a partridge in a pear tree.
 Two turtle doves,
 Three french hens,
 Four calling birds,
 Five golden rings,
 Six geese a-laying,
 Seven swans a-swimming,
 Eight maids a-milking,
 Nine ladies dancing,
 Ten lords a-leaping,
 Eleven pipers piping,
 Twelve drummers drumming,

END

sub nth($n) { say "On the @days[$n] day of Christmas, my true love gave to me:" }

nth(0); say @gifts[0].subst('And a','A');

for 1 ... 11 -> $d {

   say ;
   nth($d);
   say @gifts[$_] for $d ... 0;

}</lang>

Output:
On the first day of Christmas, my true love gave to me:
  A partridge in a pear tree.

On the second day of Christmas, my true love gave to me:
  Two turtle doves,
  And a partridge in a pear tree.

On the third day of Christmas, my true love gave to me:
  Three french hens,
  Two turtle doves,
  And a partridge in a pear tree.

On the fourth day of Christmas, my true love gave to me:
  Four calling birds,
  Three french hens,
  Two turtle doves,
  And a partridge in a pear tree.
.
.
.
On the twelfth day of Christmas, my true love gave to me:
  Twelve drummers drumming,
  Eleven pipers piping,
  Ten lords a-leaping,
  Nine ladies dancing,
  Eight maids a-milking,
  Seven swans a-swimming,
  Six geese a-laying,
  Five golden rings,
  Four calling birds,
  Three french hens,
  Two turtle doves,
  And a partridge in a pear tree.

PicoLisp

<lang PicoLisp>(de days

  first second third fourth fifth sixth
  seventh eight ninth tenth eleventh twelfth )

(de texts

  "A partridge in a pear tree."
  "Two turtle doves and"
  "Three french hens"
  "Four calling birds"
  "Five golden rings"
  "Six geese a-laying"
  "Seven swans a-swimming"
  "Eight maids a-milking"
  "Nine ladies dancing"
  "Ten lords a-leaping"
  "Eleven pipers piping"
  "Twelve drummers drumming" )

(for (X . Day) days

  (prinl "On the " Day " day of Christmas")
  (prinl "My true love game to me:")
  (for (Y X (gt0 Y) (dec Y))
     (prinl
        (if (and (= 12 X) (= 1 Y))
           "And a partridge in a pear tree."
           (get texts Y)) ) )
  (prinl) )

(bye)</lang>

PHP

<lang PHP> <?php

header("Content-Type: text/plain; charset=UTF-8");

$days = array(

   'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth',
   'tenth', 'eleventh', 'twelfth',

);

$gifts = array(

   "A partridge in a pear tree",
   "Two turtle doves",
   "Three french hens",
   "Four calling birds",
   "Five golden rings",
   "Six geese a-laying",
   "Seven swans a-swimming",
   "Eight maids a-milking",
   "Nine ladies dancing",
   "Ten lords a-leaping",
   "Eleven pipers piping",
   "Twelve drummers drumming"

);

$verses = [];

for ( $i = 0; $i < 12; $i++ ) {

   $lines = [];
   $lines[0] = "On the {$days[$i]} day of Christmas, my true love gave to me";
   
   $j = $i;
   $k = 0;
   while ( $j >= 0 ) {
       $lines[++$k] = $gifts[$j];
       $j--;
   }
       
   $verses[$i] = implode(PHP_EOL, $lines);
       
   if ( $i == 0 )
       $gifts[0] = "And a partridge in a pear tree";

}

echo implode(PHP_EOL . PHP_EOL, $verses);

?> </lang>

PowerShell

<lang PowerShell>$days = @{

   1 = "first";
   2 = "second";
   3 = "third";
   4 = "fourth";
   5 = "fifth";
   6 = "sixth";
   7 = "seventh";
   8 = "eight";
   9 = "ninth";
   10 = "tenth";
   11 = "eleventh";
   12 = "twelfth";

}

$gifts = @{

   1 = 'A partridge in a pear tree';
   2 = 'Two turtle doves';
   3 = 'Three french hens';
   4 = 'Four calling birds';
   5 = 'Five golden rings';
   6 = 'Six geese a-laying';
   7 = 'Seven swans a-swimming';
   8 = 'Eight maids a-milking';
   9 = 'Nine ladies dancing';
   10 = 'Ten lords a-leaping';
   11 = 'Eleven pipers piping';
   12 = 'Twelve drummers drumming';

}

1 .. 12 | % {

   "On the $($days[$_]) day of Christmas`nMy true love gave to me"
   $_ .. 1 | % { 
       $gift = $gifts[$_]
       if ($_ -eq 2) { $gift += " and" }
       $gift
   }
   ""

}</lang>

Output:
On the first day of Christmas
My true love gave to me
A partridge in a pear tree

On the second day of Christmas
My true love gave to me
Two turtle doves and
A partridge in a pear tree

...

On the twelfth day of Christmas
My true love gave to me
Twelve drummers drumming
Eleven pipers piping
Ten lords a-leaping
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

Python

<lang python>gifts = \ A partridge in a pear tree. Two turtle doves Three french hens Four calling birds Five golden rings Six geese a-laying Seven swans a-swimming Eight maids a-milking Nine ladies dancing Ten lords a-leaping Eleven pipers piping Twelve drummers drumming.split('\n')

days = first second third fourth fifth

         sixth seventh eighth ninth tenth
         eleventh twelfth.split()

for n, day in enumerate(days, 1):

   g = gifts[:n][::-1]
   print(('\nOn the %s day of Christmas\nMy true love gave to me:\n' % day) +
         '\n'.join(g[:-1]) +
         (' and\n' + g[-1] if n > 1 else g[-1].capitalize()))</lang>
Output:
On the first day of Christmas
My true love gave to me:
A partridge in a pear tree.

On the second day of Christmas
My true love gave to me:
Two turtle doves and
A partridge in a pear tree.

On the third day of Christmas
My true love gave to me:
Three french hens
Two turtle doves and
A partridge in a pear tree.

On the fourth day of Christmas
My true love gave to me:
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree.
.
.
.

On the twelfth day of Christmas
My true love gave to me:
Twelve drummers drumming
Eleven pipers piping
Ten lords a-leaping
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree.

Racket

This version:

  • doesn't capitalise the word Twelfth
  • capitalises the French   (in French hen)
  • capitalised True Love as it may save me a lot of grief when I get home.
  • British Variant: changes golden to go-old rings. Anyone who still has enough breath left to sing the second syllable after sustaining the first syllable of golden simply wasn't making enough effort in the first place.
  • British Variant: capitalises FIVE GO-OLD RINGS since it needs to be sung at top volume. If you want to change this back; the source is there. But I guarantee you won't have as much fun singing it.

<lang racket>#lang racket (define (ordinal-text d)

 (vector-ref
  (vector
   "zeroth" "first" "second" "third" "fourth"
   "fifth" "sixth" "seventh" "eighth" "ninth"
   "tenth" "eleventh" "twelfth")
  d))

(define (on-the... day)

 (printf "On the ~a day of Christmas,~%" (ordinal-text day))
 (printf "My True Love gave to me,~%"))

(define (prezzy prezzy-line day)

 (match prezzy-line
   [1 (string-append (if (= day 1) "A " "And a")" partridge in a pear tree")]
   [2 "Two turtle doves"]
   [3 "Three French hens"]
   [4 "Four calling birds"]
   [5 "FIVE GO-OLD RINGS"]
   [6 "Six geese a-laying"]
   [7 "Seven swans a-swimming"]
   [8 "Eight maids a-milking"]
   [9 "Nine ladies dancing"]
   [10 "Ten lords a-leaping"]
   [11 "Eleven pipers piping"]
   [12 "Twelve drummers drumming"]))

(define (line-end prezzy-line day)

 (match* (day prezzy-line) [(12 1) "."] [(x 1) ".\n"] [(_ _) ","]))

(for ((day (sequence-map add1 (in-range 12)))

     #:when (on-the... day)
     (prezzy-line (in-range day 0 -1)))
 (printf "~a~a~%" (prezzy prezzy-line day) (line-end prezzy-line day)))</lang>
Output:
On the first day of Christmas,
My True Love gave to me,
A  partridge in a pear tree.

On the second day of Christmas,
My True Love gave to me,
Two turtle doves,
And a partridge in a pear tree.

On the third day of Christmas,
My True Love gave to me,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the fourth day of Christmas,
My True Love gave to me,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the fifth day of Christmas,
My True Love gave to me,
FIVE GO-OLD RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the sixth day of Christmas,
My True Love gave to me,
Six geese a-laying,
FIVE GO-OLD RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

On the seventh day of Christmas, ...
On the eighth day of Christmas, ...
On the ninth day of Christmas, ...
On the tenth day of Christmas, ...
On the eleventh day of Christmas, ...

On the twelfth day of Christmas,
My True Love gave to me,
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
FIVE GO-OLD RINGS,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

REXX

This version:

  •   doesn't capitalize the word   Twelfth
  •   capitalizes the   French   (in French hen)
  •   capitalized   True Love   as it (may) refer to a deity
  •   added indentation to make verses resemble song lyrics

<lang rexx>/*REXX program displays the verses of the song: "The 12 days of Christmas". */ @='first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth' pad=left(,20) /*used for indenting the shown verses.*/

      g.1= 'A partridge in a pear-tree.';     g.7 = 'Seven swans a-swimming,'
      g.2= 'Two Turtle Doves, and'      ;     g.8 = 'Eight maids a-milking,'
      g.3= 'Three French Hens,'         ;     g.9 = 'Nine ladies dancing,'
      g.4= 'Four Calling Birds,'        ;     g.10= 'Ten lords a-leaping,'
      g.5= 'Five Golden Rings,'         ;     g.11= 'Eleven pipers piping,'
      g.6= 'Six geese a-laying,'        ;     g.12= 'Twelve drummers drumming,'
 do day=1  for 12
 say pad 'On the' word(@,day) 'day of Christmas'    /*display line 1 prologue*/
 say pad 'My True Love gave to me:'                 /*   "      "  2     "   */
       do j=day  to 1  by -1;   say pad g.j;   end  /*   "    the daily gifts*/
 say                                  /*add a blank line between the verses. */
 end   /*day*/                        /*stick a fork in it,  we're all done. */</lang>
Output:
                     On the first day of Christmas
                     My True Love gave to me:
                     A partridge in a pear-tree.

                     On the second day of Christmas
                     My True Love gave to me:
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

                     On the third day of Christmas
                     My True Love gave to me:
                     Three French Hens,
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

                     On the fourth day of Christmas
                     My True Love gave to me:
                     Four Calling Birds,
                     Three French Hens,
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

                     On the fifth day of Christmas
                     My True Love gave to me:
                     Five Golden Rings,
                     Four Calling Birds,
                     Three French Hens,
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

                     On the sixth day of Christmas
                     My True Love gave to me:
                     Six geese a-laying,
                     Five Golden Rings,
                     Four Calling Birds,
                     Three French Hens,
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

                     On the seventh day of Christmas
                     My True Love gave to me:
                     Seven swans a-swimming,
                     Six geese a-laying,
                     Five Golden Rings,
                     Four Calling Birds,
                     Three French Hens,
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

                     On the eighth day of Christmas
                     My True Love gave to me:
                     Eight maids a-milking,
                     Seven swans a-swimming,
                     Six geese a-laying,
                     Five Golden Rings,
                     Four Calling Birds,
                     Three French Hens,
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

                     On the ninth day of Christmas
                     My True Love gave to me:
                     Nine ladies dancing,
                     Eight maids a-milking,
                     Seven swans a-swimming,
                     Six geese a-laying,
                     Five Golden Rings,
                     Four Calling Birds,
                     Three French Hens,
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

                     On the tenth day of Christmas
                     My True Love gave to me:
                     Ten lords a-leaping,
                     Nine ladies dancing,
                     Eight maids a-milking,
                     Seven swans a-swimming,
                     Six geese a-laying,
                     Five Golden Rings,
                     Four Calling Birds,
                     Three French Hens,
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

                     On the eleventh day of Christmas
                     My True Love gave to me:
                     Eleven pipers piping,
                     Ten lords a-leaping,
                     Nine ladies dancing,
                     Eight maids a-milking,
                     Seven swans a-swimming,
                     Six geese a-laying,
                     Five Golden Rings,
                     Four Calling Birds,
                     Three French Hens,
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

                     On the twelfth day of Christmas
                     My True Love gave to me:
                     Twelve drummers drumming,
                     Eleven pipers piping,
                     Ten lords a-leaping,
                     Nine ladies dancing,
                     Eight maids a-milking,
                     Seven swans a-swimming,
                     Six geese a-laying,
                     Five Golden Rings,
                     Four Calling Birds,
                     Three French Hens,
                     Two Turtle Doves, and
                     A partridge in a pear-tree.

Ruby

<lang ruby>gifts = "A partridge in a pear tree Two turtle doves and Three french hens Four calling birds Five golden rings Six geese a-laying Seven swans a-swimming Eight maids a-milking Nine ladies dancing Ten lords a-leaping Eleven pipers piping Twelve drummers drumming".split("\n")

days = %w(first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth)

days.each_with_index do |day, i|

 puts "On the #{day} day of Christmas"
 puts "My true love gave to me:"
 puts gifts[0, i+1].reverse
 puts

end</lang>

Run BASIC

<lang Runbasic>gifts$ = " A partridge in a pear tree., Two turtle doves, Three french hens, Four calling birds, Five golden rings, Six geese a-laying, Seven swans a-swimming, Eight maids a-milking, Nine ladies dancing, Ten lords a-leaping, Eleven pipers piping, Twelve drummers drumming"

days$ = "first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth"

for i = 1 to 12

   print "On the ";word$(days$,i," ");" day of Christmas"
   print "My true love gave to me:"
   for j = i to 1 step -1
   if i > 1 and j = 1 then print "and ";
   print mid$(word$(gifts$,j,","),2)
   next j
   print

next i </lang>Output:

On the first day of Christmas
My true love gave to me:
A partridge in a pear tree.
.
.
On the twelfth day of Christmas
My true love gave to me:
Twelve drummers drumming
Eleven pipers piping
Ten lords a-leaping
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves
and A partridge in a pear tree.

Rust

<lang Rust>fn showpresents(count : usize) {

   let days = ["second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
               "tenth", "eleventh", "twelfth"];
   let presents = ["Two turtle doves",
                   "Three french hens",
                   "Four calling birds",
                   "Five golden rings",
                   "Six geese a-laying",
                   "Seven swans a-swimming",
                   "Eight maids a-milking",
                   "Nine ladies dancing",
                   "Ten lords a-leaping",
                   "Eleven pipers piping",
                   "Twelve drummers drumming"];
   println!("On the {} day of Christmas my true love gave to me {}", days[count-1], presents[count-1]);
   if count > 0 {
       let mut j = count-1;
       while j > 0 {
           println!("{}", presents[j-1]);
           j -= 1;
       }
   }
   println!("And a partridge in a pear tree \n");

}

fn main() {

   let mut count = 0;
   while count < 12 {
       match count {
           0 => println!("On the first day of Christmas my true love gave to me a partridge in a pear tree"),
           _ => showpresents(count)
       };
       count += 1;
   }

}</lang>

Scala

<lang scala>val gifts = Array(

   "A partridge in a pear tree.",
   "Two turtle doves and",
   "Three french hens,",
   "Four calling birds,",
   "Five gold rings,",
   "Six geese a-laying,",
   "Seven swans a-swimming,",
   "Eight maids a-milking,",
   "Nine ladies dancing,",
   "Ten lords a-leaping,",
   "Eleven pipers piping,",
   "Twelve drummers drumming,"
 )

val days = Array(

   "first",   "second", "third", "fourth", "fifth",    "sixth",
   "seventh", "eighth", "ninth", "tenth",  "eleventh", "twelfth"
 )

val giftsForDay = (day: Int) =>

   "On the %s day of Christmas, my true love gave to me:\n".format(days(day)) +
     gifts.take(day+1).reverse.mkString("\n") + "\n"

(0 until 12).map(giftsForDay andThen println)</lang>

Output:
On the first day of Christmas, my true love gave to me:
A partridge in a pear tree

On the second day of Christmas, my true love gave to me:
Two turtle doves and
A partridge in a pear tree

On the third day of Christmas, my true love gave to me:
Three french hens
Two turtle doves and
A partridge in a pear tree

On the fourth day of Christmas, my true love gave to me:
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

On the fifth day of Christmas, my true love gave to me:
FIVE GOLDEN RINGS
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

On the sixth day of Christmas, my true love gave to me:
Six geese a-laying
FIVE GOLDEN RINGS
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

On the seventh day of Christmas, my true love gave to me:
Seven swans a-swimming
Six geese a-laying
FIVE GOLDEN RINGS
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

On the eighth day of Christmas, my true love gave to me:
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
FIVE GOLDEN RINGS
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

On the ninth day of Christmas, my true love gave to me:
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
FIVE GOLDEN RINGS
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

On the tenth day of Christmas, my true love gave to me:
Ten lords a-leaping
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
FIVE GOLDEN RINGS
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

On the eleventh day of Christmas, my true love gave to me:
Eleven pipers piping
Ten lords a-leaping
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
FIVE GOLDEN RINGS
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

On the twelfth day of Christmas, my true love gave to me:
Twelve drummers drumming
Eleven pipers piping
Ten lords a-leaping
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
FIVE GOLDEN RINGS
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree

Seed7

<lang seed7>$ include "seed7_05.s7i";

const proc: main is func

 local
   const array string: gifts is [] (
       "A partridge in a pear tree.", "Two turtle doves and",
       "Three french hens", "Four calling birds",
       "Five golden rings", "Six geese a-laying",
       "Seven swans a-swimming", "Eight maids a-milking",
       "Nine ladies dancing", "Ten lords a-leaping",
       "Eleven pipers piping", "Twelve drummers drumming");
   const array string: days is [] (
       "first", "second", "third", "fourth", "fifth", "sixth",
       "seventh", "eighth", "ninth", "tenth", "eleventh", "Twelfth");
   var integer: day is 0;
   var integer: gift is 0;
 begin
   for day range 1 to length(days) do
     writeln;
     writeln("On the " <& days[day] <& " day of Christmas,");
     writeln("My true love gave to me:");
     for gift range day downto 1 do
       writeln(gifts[gift]);
     end for;
   end for;
 end func;</lang>
Output:
On the first day of Christmas,
My true love gave to me:
A partridge in a pear tree.

On the second day of Christmas,
My true love gave to me:
Two turtle doves and
A partridge in a pear tree.

On the third day of Christmas,
My true love gave to me:
Three french hens
Two turtle doves and
A partridge in a pear tree.

...

On the Twelfth day of Christmas,
My true love gave to me:
Twelve drummers drumming
Eleven pipers piping
Ten lords a-leaping
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree.

Self

Nicely factored: <lang self>(| parent* = traits oddball.

gifts = (

  'And a partridge in a pear tree' &
  'Two turtle doves' &
  'Three french hens' &
  'Four calling birds' &
  'FIVE GO-OLD RINGS' &
  'Six geese a-laying' &
  'Seven swans a-swimming' &
  'Eight maids a-milking' &
  'Nine ladies dancing' &
  'Ten lords a-leaping' &
  'Eleven pipers piping' &
  'Twelve drummers drumming'

) asSequence.

days = (

  'first' & 'second' & 'third'    & 'fourth'  & 
  'fifth' & 'sixth'  & 'seventh'  & 'eighth'  &
  'ninth' & 'tenth'  & 'eleventh' & 'twelfth'

) asSequence.

intro: i = ( 'On the ', (days at: i), ' day of Christmas, my true love gave to me:'). gifts: i = ( i = 0 ifTrue: [sequence copyAddFirst: 'A partridge in a pear tree' ]

                    False: [(gifts slice: 0@(i + 1)) reverse ]).

verse: i = ( ((sequence copyAddFirst: intro: i) addAll: gifts: i) addLast: ). value = ( (days gather: [|:d. :i| verse: i ]) asSequence joinUsing: '\n' )

|) value printLine </lang>

Sidef

Translation of: Perl 6

<lang ruby>var days = <first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth>;

var gifts = <<'EOT'.lines;

 And a partridge in a pear tree.
 Two turtle doves,
 Three french hens,
 Four calling birds,
 Five golden rings,
 Six geese a-laying,
 Seven swans a-swimming,
 Eight maids a-milking,
 Nine ladies dancing,
 Ten lords a-leaping,
 Eleven pipers piping,
 Twelve drummers drumming,

EOT

func nth(n) { say "On the #{days[n]} day of Christmas, my true love gave to me:" };

nth(0); say gifts[0].sub('And a', 'A');

range(1, 11).each { |d|

   say ;
   nth(d);
   d.downto(0).each { |i|
       say gifts[i];
   }

}</lang>

Output:
On the first day of Christmas, my true love gave to me:
  A partridge in a pear tree.

On the second day of Christmas, my true love gave to me:
  Two turtle doves,
  And a partridge in a pear tree.

On the third day of Christmas, my true love gave to me:
  Three french hens,
  Two turtle doves,
  And a partridge in a pear tree.

On the fourth day of Christmas, my true love gave to me:
  Four calling birds,
  Three french hens,
  Two turtle doves,
  And a partridge in a pear tree.
.
.
.
On the twelfth day of Christmas, my true love gave to me:
  Twelve drummers drumming,
  Eleven pipers piping,
  Ten lords a-leaping,
  Nine ladies dancing,
  Eight maids a-milking,
  Seven swans a-swimming,
  Six geese a-laying,
  Five golden rings,
  Four calling birds,
  Three french hens,
  Two turtle doves,
  And a partridge in a pear tree.

SQL

Demonstration of Oracle 12c "with" clause enhancement.

<lang SQL> with function nl ( s in varchar2 ) return varchar2 is begin

       return chr(10) || s;

end nl; function v ( d number, x number, g in varchar2 ) return varchar2 is begin

       return
       case when d >= x then nl (g) end;

end v; select 'On the '

       || case level
       when 1 then 'first'
       when 2 then 'second'
       when 3 then 'third'
       when 4 then 'fourth'
       when 5 then 'fifth'
       when 6 then 'sixth'
       when 7 then 'seventh'
       when 8 then 'eighth'
       when 9 then 'ninth'
       when 10 then 'tenth'
       when 11 then 'eleventh'
       when 12 then 'twelfth'
       end
       || ' of Christmas,'
       || nl( 'My true love gave to me:')
       || v ( level, 12, 'Twelve drummers drumming' )
       || v ( level, 11, 'Eleven pipers piping' )
       || v ( level, 10, 'Ten lords a-leaping' )
       || v ( level, 9, 'Nine ladies dancing' )
       || v ( level, 8, 'Eight maids a-milking' )
       || v ( level, 7, 'Seven swans a-swimming' )
       || v ( level, 6, 'Six geese a-laying' )
       || v ( level, 5, 'Five golden rings!' )
       || v ( level, 4, 'Four calling birds' )
       || v ( level, 3, 'Three French hens' )
       || v ( level, 2, 'Two turtle doves' )
       || v ( level, 1, case level when 1 then 'A' else 'And a' end || ' partridge in a pear tree.' )
       || nl(null)
       "The Twelve Days of Christmas"

from dual connect by level <= 12 / </lang> output:

The Twelve Days of Christmas
-------------------------------------
On the first of Christmas,
My true love gave to me:
A partridge in a pear tree.

...

On the twelfth of Christmas,
My true love gave to me:
Twelve drummers drumming
Eleven pipers piping
Ten lords a-leaping
Nine ladies dancing
Eight maids a-milking
Seven swans a-swimming
Six geese a-laying
Five golden rings!
Four calling birds
Three French hens
Two turtle doves
And a partridge in a pear tree.

Swift

Works with: Swift version 2.1

<lang swift>let gifts = [ "partridge in a pear tree", "Two turtle doves",

             "Three French hens",        "Four calling birds",       
             "Five gold rings",          "Six geese a-laying", 
             "Seven swans a-swimming",   "Eight maids a-milking",
             "Nine ladies dancing",      "Ten lords a-leaping", 
             "Eleven pipers piping",     "Twelve drummers drumming" ]

let nth = [ "first", "second", "third", "fourth", "fifth", "sixth",

           "seventh", "eighth", "ninth", "tenth",  "eleventh", "twelfth" ]

func giftsForDay(day: Int) -> String {

 var result = "On the \(nth[day-1]) day of Christmas, my true love gave to me:\n"
 if day > 1 {
   for again in 1...day-1 {
     let n = day - again 
     result += gifts[n]
     if n > 1 { result += "," }
     result += "\n"
   }
   result += "And a "
 } else {
   result += "A "
 }
 return result + gifts[0] + ".\n";

}

for day in 1...12 {

 print(giftsForDay(day))

}</lang>

Output:
On the first day of Christmas, my true love gave to me:
A partridge in a pear tree.

On the second day of Christmas, my true love gave to me:
Two turtle doves
And a partridge in a pear tree.

On the third day of Christmas, my true love gave to me:
Three French hens,
Two turtle doves
And a partridge in a pear tree.

On the fourth day of Christmas, my true love gave to me:
Four calling birds,
Three French hens,
Two turtle doves
And a partridge in a pear tree.

On the fifth day of Christmas, my true love gave to me:
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves
And a partridge in a pear tree.

On the sixth day of Christmas, my true love gave to me:
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves
And a partridge in a pear tree.

On the seventh day of Christmas, my true love gave to me:
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves
And a partridge in a pear tree.

On the eighth day of Christmas, my true love gave to me:
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves
And a partridge in a pear tree.

On the ninth day of Christmas, my true love gave to me:
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves
And a partridge in a pear tree.

On the tenth day of Christmas, my true love gave to me:
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves
And a partridge in a pear tree.

On the eleventh day of Christmas, my true love gave to me:
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves
And a partridge in a pear tree.

On the twelfth day of Christmas, my true love gave to me:
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves
And a partridge in a pear tree.

Tcl

Works with: Tcl version 8.6

<lang tcl>set days {

   first second third fourth fifth sixth
   seventh eighth ninth tenth eleventh twelfth

} set gifts [lreverse {

   "A partridge in a pear tree."
   "Two turtle doves, and"
   "Three french hens,"
   "Four calling birds,"
   "Five golden rings,"
   "Six geese a-laying,"
   "Seven swans a-swimming,"
   "Eight maids a-milking,"
   "Nine ladies dancing,"
   "Ten lords a-leaping,"
   "Eleven pipers piping,"
   "Twelve drummers drumming,"

}]

set n -1;puts [join [lmap day $days {

   format "On the $day day of Christmas,\nMy true love gave to me:\n%s" \

[join [lrange $gifts end-[incr n] end] \n] }] \n\n]</lang>

Output:
On the first day of Christmas,
My true love gave to me:
A partridge in a pear tree.

On the second day of Christmas,
My true love gave to me:
Two turtle doves, and
A partridge in a pear tree.

On the third day of Christmas,
My true love gave to me:
Three french hens,
Two turtle doves, and
A partridge in a pear tree.

On the fourth day of Christmas,
My true love gave to me:
Four calling birds,
Three french hens,
Two turtle doves, and
A partridge in a pear tree.

On the fifth day of Christmas,
My true love gave to me:
Five golden rings,
Four calling birds,
Three french hens,
Two turtle doves, and
A partridge in a pear tree.

On the sixth day of Christmas,
My true love gave to me:
Six geese a-laying,
Five golden rings,
Four calling birds,
Three french hens,
Two turtle doves, and
A partridge in a pear tree.

On the seventh day of Christmas,
My true love gave to me:
Seven swans a-swimming,
Six geese a-laying,
Five golden rings,
Four calling birds,
Three french hens,
Two turtle doves, and
A partridge in a pear tree.

On the eighth day of Christmas,
My true love gave to me:
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five golden rings,
Four calling birds,
Three french hens,
Two turtle doves, and
A partridge in a pear tree.

On the ninth day of Christmas,
My true love gave to me:
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five golden rings,
Four calling birds,
Three french hens,
Two turtle doves, and
A partridge in a pear tree.

On the tenth day of Christmas,
My true love gave to me:
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five golden rings,
Four calling birds,
Three french hens,
Two turtle doves, and
A partridge in a pear tree.

On the eleventh day of Christmas,
My true love gave to me:
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five golden rings,
Four calling birds,
Three french hens,
Two turtle doves, and
A partridge in a pear tree.

On the twelfth day of Christmas,
My true love gave to me:
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five golden rings,
Four calling birds,
Three french hens,
Two turtle doves, and
A partridge in a pear tree.

UNIX Shell

Works with: Bourne Again SHell
Works with: Korn Shell version 93

<lang bash>#!/usr/bin/env bash ordinals=(first second third fourth fifth sixth

         seventh eighth ninth tenth  eleventh twelfth)

gifts=( "A partridge in a pear tree." "Two turtle doves and"

       "Three French hens,"          "Four calling birds,"
       "Five gold rings,"            "Six geese a-laying,"
       "Seven swans a-swimming,"     "Eight maids a-milking,"
       "Nine ladies dancing,"        "Ten lords a-leaping,"
       "Eleven pipers piping,"       "Twelve drummers drumming," )

echo_gifts() {

 local i day=$1
 echo "On the ${ordinals[day]} day of Christmas, my true love sent to me:"
 for (( i=day; i >=0; --i )); do
   echo "${gifts[i]}"
 done
 echo

}

for (( day=0; day < 12; ++day )); do

 echo_gifts $day

done</lang>

The above will also work in zsh if the index range is changed from 0..11 to 1..12.

Works with: Bourne Shell

(requires the seq command)

<lang sh>#!/bin/sh ordinal() {

 n=$1
 set first   second third fourth fifth    sixth \
     seventh eighth ninth tenth  eleventh twelfth
 shift $n
 echo $1

}

gift() {

 n=$1
 set "A partridge in a pear tree." "Two turtle doves and"      \
     "Three French hens,"          "Four calling birds,"       \
     "Five gold rings,"            "Six geese a-laying,"       \
     "Seven swans a-swimming,"     "Eight maids a-milking,"    \
     "Nine ladies dancing,"        "Ten lords a-leaping,"      \
     "Eleven pipers piping,"       "Twelve drummers drumming," 
 shift $n
 echo "$1"

}

echo_gifts() {

 day=$1
 echo "On the `ordinal $day` day of Christmas, my true love sent to me:"
 for i in `seq $day 0`; do
   gift $i
 done
 echo

}

for day in `seq 0 11`; do

 echo_gifts $day

done</lang>

Output:
On the first day of Christmas, my true love sent to me:
A partridge in a pear tree.

On the second day of Christmas, my true love sent to me:
Two turtle doves and
A partridge in a pear tree.

On the third day of Christmas, my true love sent to me:
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the fourth day of Christmas, my true love sent to me:
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the fifth day of Christmas, my true love sent to me:
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the sixth day of Christmas, my true love sent to me:
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the seventh day of Christmas, my true love sent to me:
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the eighth day of Christmas, my true love sent to me:
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the ninth day of Christmas, my true love sent to me:
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the tenth day of Christmas, my true love sent to me:
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the eleventh day of Christmas, my true love sent to me:
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.

On the twelfth day of Christmas, my true love sent to me:
Twelve drummers drumming,
Eleven pipers piping,
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves and
A partridge in a pear tree.



VBScript

<lang vb>days = Array("first","second","third","fourth","fifth","sixth",_ "seventh","eight","ninth","tenth","eleventh","twelfth")

gifts = Array("A partridge in a pear tree","Two turtle doves","Three french hens",_ "Four calling birds","Five golden rings","Six geese a-laying","Seven swans a-swimming",_ "Eight maids a-milking","Nine ladies dancing","Ten lords a-leaping","Eleven pipers piping",_ "Twelve drummers drumming")

For i = 0 To 11 WScript.StdOut.Write "On the " & days(i) & " day of Christmas" WScript.StdOut.WriteLine WScript.StdOut.Write "My true love sent to me:" WScript.StdOut.WriteLine If i = 0 Then WScript.StdOut.Write gifts(i) Else For j = i To 0 Step - 1 If j = 0 Then WScript.StdOut.Write "and " & gifts(0) Else WScript.StdOut.Write gifts(j) WScript.StdOut.WriteLine End If Next End If WScript.StdOut.WriteBlankLines(2) Next</lang>

zkl

Translation of: Python

<lang zkl>gifts:=

  1. <<<

"A beer, in a tree.; Two turtlenecks; Three french toast; Four pounds of backbacon; Five golden touques; Six packs of two-four; Seven packs of smokes; Eight comic books; Nine back up singers; Ten feet of snow; Eleven hosers hosing; Twelve dozen donuts"

  1. <<<

.split(";").apply("strip");

days:=("first second third fourth fifth sixth seventh eighth ninth tenth "

     "eleventh twelfth").split();

foreach n,day in (days.enumerate()){ n+=1;

  g:=gifts[0,n].reverse();
  println("On the %s day of Christmas\nMy true love gave to me:\n".fmt(day),
        g[0,-1].concat("\n"), (n>1) and " and\n" or "", g[-1], "\n");

}</lang>

Output:
On the first day of Christmas
My true love gave to me:
A beer, in a tree.

On the second day of Christmas
My true love gave to me:
Two turtlenecks and
A beer, in a tree.

On the third day of Christmas
My true love gave to me:
Three french toast
Two turtlenecks and
A beer, in a tree.

On the fourth day of Christmas
My true love gave to me:
Four pounds of backbacon
Three french toast
Two turtlenecks and
A beer, in a tree.

On the fifth day of Christmas
My true love gave to me:
Five golden touques
Four pounds of backbacon
Three french toast
Two turtlenecks and
A beer, in a tree.

On the sixth day of Christmas
My true love gave to me:
Six packs of two-four
Five golden touques
Four pounds of backbacon
Three french toast
Two turtlenecks and
A beer, in a tree.

On the seventh day of Christmas
My true love gave to me:
Seven packs of smokes
Six packs of two-four
Five golden touques
Four pounds of backbacon
Three french toast
Two turtlenecks and
A beer, in a tree.

On the eighth day of Christmas
My true love gave to me:
Eight comic books
Seven packs of smokes
Six packs of two-four
Five golden touques
Four pounds of backbacon
Three french toast
Two turtlenecks and
A beer, in a tree.

On the ninth day of Christmas
My true love gave to me:
Nine back up singers
Eight comic books
Seven packs of smokes
Six packs of two-four
Five golden touques
Four pounds of backbacon
Three french toast
Two turtlenecks and
A beer, in a tree.

On the tenth day of Christmas
My true love gave to me:
Ten feet of snow
Nine back up singers
Eight comic books
Seven packs of smokes
Six packs of two-four
Five golden touques
Four pounds of backbacon
Three french toast
Two turtlenecks and
A beer, in a tree.

On the eleventh day of Christmas
My true love gave to me:
Eleven hosers hosing
Ten feet of snow
Nine back up singers
Eight comic books
Seven packs of smokes
Six packs of two-four
Five golden touques
Four pounds of backbacon
Three french toast
Two turtlenecks and
A beer, in a tree.

On the twelfth day of Christmas
My true love gave to me:
Twelve dozen donuts
Eleven hosers hosing
Ten feet of snow
Nine back up singers
Eight comic books
Seven packs of smokes
Six packs of two-four
Five golden touques
Four pounds of backbacon
Three french toast
Two turtlenecks and
A beer, in a tree.