🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of n0mn0m

n0mn0m's solution

to Twelve Days in the F# Track

Published at Nov 05 2020 · 0 comments
Instructions
Test suite
Solution

Output the lyrics to 'The Twelve Days of Christmas'.

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.

Hints

  • Try to capture the structure of the song in your code, where you build up the song by composing its parts.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Autoformatting the code

F# source code can be formatted with the Fantomas tool.

After installing it with dotnet tool restore, run dotnet fantomas . to format code within the current directory.

Further information

For more detailed information about the F# track, including how to get help if you're having trouble, please visit the exercism.io F# language page.

Source

Wikipedia http://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_(song)

TwelveDaysTests.fs

// This file was auto-generated based on version 1.2.0 of the canonical data.

module TwelveDaysTests

open FsUnit.Xunit
open Xunit

open TwelveDays

[<Fact>]
let ``First day a partridge in a pear tree`` () =
    let expected = ["On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree."]
    recite 1 1 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Second day two turtle doves`` () =
    let expected = ["On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree."]
    recite 2 2 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Third day three french hens`` () =
    let expected = ["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."]
    recite 3 3 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Fourth day four calling birds`` () =
    let expected = ["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."]
    recite 4 4 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Fifth day five gold rings`` () =
    let expected = ["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."]
    recite 5 5 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Sixth day six geese-a-laying`` () =
    let expected = ["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."]
    recite 6 6 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Seventh day seven swans-a-swimming`` () =
    let expected = ["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."]
    recite 7 7 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Eighth day eight maids-a-milking`` () =
    let expected = ["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."]
    recite 8 8 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Ninth day nine ladies dancing`` () =
    let expected = ["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."]
    recite 9 9 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Tenth day ten lords-a-leaping`` () =
    let expected = ["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."]
    recite 10 10 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Eleventh day eleven pipers piping`` () =
    let expected = ["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."]
    recite 11 11 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Twelfth day twelve drummers drumming`` () =
    let expected = ["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."]
    recite 12 12 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Recites first three verses of the song`` () =
    let expected = 
        [ "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." ]
    recite 1 3 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Recites three verses from the middle of the song`` () =
    let expected = 
        [ "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." ]
    recite 4 6 |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Recites the whole song`` () =
    let expected = 
        [ "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." ]
    recite 1 12 |> should equal expected
module TwelveDays

let nth =
    [ "first"; "second"; "third"; "fourth"; "fifth"; "sixth"; "seventh"; "eighth"; "ninth"; "tenth"; "eleventh"; "twelfth" ]

let nthObject =
    [ "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-Milking"
      "nine Ladies Dancing"
      "ten Lords-a-Leaping"
      "eleven Pipers Piping"
      "twelve Drummers Drumming" ]

let rec verseObject n =
    let part = nthObject.[n - 1]
    match n with
    | 1 -> part
    | 2 -> sprintf "%s, and %s" part (verseObject (n - 1))
    | _ -> sprintf "%s, %s" part (verseObject (n - 1))

let composeVerse n = sprintf "On the %s day of Christmas my true love gave to me: %s." nth.[n - 1] (verseObject n)

let recite start stop =
    seq { start .. stop }
    |> Seq.map composeVerse
    |> Seq.toList

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?