Task: Old lady swallowed a fly






PHP: <?php

$swallowed = array(
  array('swallowed' => 'fly.',
        'reason' => "I don't know why she swallowed the fly."),
  array('swallowed' => 'spider,',
        'aside' => "which wiggled and jiggled and tickled inside her.",
        'reason' => "She swallowed the spider to catch the fly"),
  array('swallowed' => 'bird.',
        'aside' => "How absurd! To swallow a bird!",
        'reason' => "She swallowed the bird to catch the spider,"),
  array('swallowed' => 'cat.',
        'aside' => "Imagine that! To swallow a cat!",
        'reason' => "She swallowed the cat to catch the bird."),
  array('swallowed' => 'dog.',
        'aside' => "What a hog! To swallow a dog!",
        'reason' => "She swallowed the dog to catch the cat."),
  array('swallowed' => 'horse',
        'aside' => "She's dead, of course. She swallowed a horse!",
        'reason' => "She swallowed the horse to catch the dog."));

foreach($swallowed as $creature)
{
  print "I knew an old lady who swallowed a " . $creature['swallowed'] . "\n";
  if(array_key_exists('aside', $creature))
    print $creature['aside'] . "\n";

  $reversed = array_reverse($swallowed);
  $history = array_slice($reversed, array_search($creature, $reversed));

  foreach($history as $note)
  {
    print $note['reason'] . "\n";
  }

  if($swallowed[count($swallowed) - 1] == $creature)
    print "But she sure died!\n";
  else
    print "Perhaps she'll die." . "\n\n";
}






Ada: with Ada.Text_IO, Ada.Containers.Indefinite_Doubly_Linked_Lists; use Ada.Text_IO;

procedure Swallow_Fly is

   package Strings is new Ada.Containers.Indefinite_Doubly_Linked_Lists(String);

   Lines, Animals: Strings.List;

   procedure Swallow(Animal: String;
                     Second_Line: String;
                     Permanent_Second_Line: Boolean := True) is

      procedure Print(C: Strings.Cursor) is
      begin
         Put_Line(Strings.Element(C));
      end Print;

   begin
      Put_Line("There was an old lady who swallowed a " & Animal & ",");
      Put_Line(Second_Line);
      if not Animals.Is_Empty then
         Lines.Prepend("She swallowed the " & Animal & " to catch the " &
			 Animals.Last_Element & ",");
      end if;
      Lines.Iterate(Print'Access);
      New_Line;
      if Permanent_Second_Line then
         Lines.Prepend(Second_Line);
      end if;
      Animals.Append(Animal); 
   end Swallow;

   procedure Swallow_TSA(Animal: String; Part_Of_Line_2: String) is
   begin
      Swallow(Animal, Part_Of_Line_2 &", to swallow a " & Animal & ";", False);
   end Swallow_TSA;

   procedure Swallow_SSA(Animal: String; Part_Of_Line_2: String) is
   begin
      Swallow(Animal, Part_Of_Line_2 &" she swallowed a " & Animal & ";", False);
   end Swallow_SSA;

begin
   Lines.Append("Perhaps she'll die!");

   Swallow("fly", "But I don't know why she swallowed the fly,");
   Swallow("spider",  "That wriggled and jiggled and tickled inside her;");
   Swallow_TSA("bird", "Quite absurd");
   Swallow_TSA("cat", "Fancy that");
   Swallow_TSA("dog", "What a hog");
   Swallow_TSA("pig", "Her mouth was so big");
   Swallow_TSA("goat","She just opened her throat");
   Swallow_SSA("cow", "I don't know how");
   Swallow_TSA("donkey", "It was rather wonky");

   Put_Line("There was an old lady who swallowed a horse ...");
   Put_Line("She's dead, of course!");
end Swallow_Fly;