Skip to content

Added twelve-days haskell exercise #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 4, 2018
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@
"Maybe"
]
},
{
"uuid": "d1dc946d-a142-42ed-abec-e53605635c0d",
"slug": "twelve-days",
"core": false,
"unlocked_by": null,
"difficulty": 3,
"topics": [
]
},
{
"uuid": "382e4fbd-99d6-400b-962c-ebb4a411bcea",
"slug": "beer-song",
Expand Down
89 changes: 89 additions & 0 deletions exercises/twelve-days/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Twelve Days

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

```text
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.
```


## Getting Started

For installation and learning resources, refer to the
[exercism help page](http://exercism.io/languages/haskell).

## Running the tests

To run the test suite, execute the following command:

```bash
stack test
```

#### If you get an error message like this...

```
No .cabal file found in directory
```

You are probably running an old stack version and need
to upgrade it.

#### Otherwise, if you get an error message like this...

```
No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
```

Just do as it says and it will download and install
the correct compiler version:

```bash
stack setup
```

## Running *GHCi*

If you want to play with your solution in GHCi, just run the command:

```bash
stack ghci
```

## Feedback, Issues, Pull Requests

The [exercism/haskell](https://github.com/exercism/haskell) repository on
GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new
one, head over there and create an issue. We'll do our best to help you!

## Source

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

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
16 changes: 16 additions & 0 deletions exercises/twelve-days/examples/success-standard/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: twelve-days

dependencies:
- base

library:
exposed-modules: TwelveDays
source-dirs: src

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- twelve-days
- hspec
36 changes: 36 additions & 0 deletions exercises/twelve-days/examples/success-standard/src/TwelveDays.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module TwelveDays (recite) where

recite :: Int -> Int -> [String]
recite start stop
| start <= stop = lyrics start : recite (start + 1) stop
| otherwise = []

lyrics :: Int -> String
lyrics days =
beginning ++ end days
where
beginning =
let (day, _) = verse days
in "On the " ++ day ++ " day of Christmas my true love gave to me"
end days'
| days < 13 && days' > 0 = let (_, gift) = verse days'
in prefix days' ++ gift ++ end (days' - 1)
| otherwise = "."
where
prefix 1 = if days > 1 then ", and " else ", "
prefix _ = ", "

verse :: Int -> (String, String)
verse 12 = ("twelfth", "twelve Drummers Drumming")
verse 11 = ("eleventh", "eleven Pipers Piping")
verse 10 = ("tenth", "ten Lords-a-Leaping")
verse 9 = ("ninth", "nine Ladies Dancing")
verse 8 = ("eighth", "eight Maids-a-Milking")
verse 7 = ("seventh", "seven Swans-a-Swimming")
verse 6 = ("sixth", "six Geese-a-Laying")
verse 5 = ("fifth", "five Gold Rings")
verse 4 = ("fourth", "four Calling Birds")
verse 3 = ("third", "three French Hens")
verse 2 = ("second", "two Turtle Doves")
verse 1 = ("first", "a Partridge in a Pear Tree")
verse _ = ("", "")
20 changes: 20 additions & 0 deletions exercises/twelve-days/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: twelve-days
version: 1.0.0.1

dependencies:
- base

library:
exposed-modules: TwelveDays
source-dirs: src
dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- twelve-days
- hspec
4 changes: 4 additions & 0 deletions exercises/twelve-days/src/TwelveDays.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module TwelveDays (recite) where

recite :: Int -> Int -> [String]
recite start stop = error "You need to implement this function."
1 change: 1 addition & 0 deletions exercises/twelve-days/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resolver: lts-9.11
146 changes: 146 additions & 0 deletions exercises/twelve-days/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{-# LANGUAGE RecordWildCards #-}

import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import TwelveDays (recite)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "responseFor" $ for_ cases test
where
test Case{..} = it description assertion
where
assertion = recite start stop `shouldBe` expected

data Case = Case { description :: String
, start :: Int
, stop :: Int
, expected :: [String]
}

cases :: [Case]
cases = [ Case { description = "first day a partridge in a pear tree"
, start = 1
, stop = 1
, expected = [
"On the first day of Christmas my true love gave to me, a Partridge in a Pear Tree."
]
}
, Case { description = "second day two turtle doves"
, start = 2
, stop = 2
, expected = [
"On the second day of Christmas my true love gave to me, two Turtle Doves, and a Partridge in a Pear Tree."
]
}
, Case { description = "third day three french hens"
, start = 3
, stop = 3
, 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."
]
}
, Case { description = "fourth day four calling birds"
, start = 4
, stop = 4
, 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."
]
}
, Case { description = "fifth day five gold rings"
, start = 5
, stop = 5
, 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."
]
}
, Case { description = "sixth day six geese-a-laying"
, start = 6
, stop = 6
, 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."
]
}
, Case { description = "seventh day seven swans-a-swimming"
, start = 7
, stop = 7
, 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."
]
}
, Case { description = "eighth day eight maids-a-milking"
, start = 8
, stop = 8
, 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."
]
}
, Case { description = "ninth day nine ladies dancing"
, start = 9
, stop = 9
, 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."
]
}
, Case { description = "tenth day ten lords-a-leaping"
, start = 10
, stop = 10
, 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."
]
}
, Case { description = "eleventh day eleven pipers piping"
, start = 11
, stop = 11
, 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."
]
}
, Case { description = "twelfth day twelve drummers drumming"
, start = 12
, stop = 12
, 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."
]
}
, Case { description = "recites first three verses of the song"
, start = 1
, stop = 3
, 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."
]
}
, Case { description = "recites three verses from the middle of the song"
, start = 4
, stop = 6
, 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."
]
}
, Case { description = "recites the whole song"
, start = 1
, stop = 12
, 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."
]
}
]