Last active
March 7, 2017 06:28
-
-
Save parsonsmatt/3c030058e5bfb19637c268806bc51c38 to your computer and use it in GitHub Desktop.
26 Bottles of Functional Programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Beer where | |
import Data.Char (toUpper) | |
main = putStrLn (song [100, 99 .. 0]) | |
song = unlines . map verse | |
verse x = unlines [firstLine x, secondLine x] | |
firstLine x = unwords [capIfZero (beerOnTheWall x) ++ ",", beer x, "of beer."] | |
where | |
capIfZero (c:cs) = if x == 0 then toUpper c : cs else c : cs | |
secondLine 0 = "Go to the store and buy some more, " ++ beerOnTheWall 99 ++ "!" | |
secondLine n = unwords ["Take", it, "down and pass it around,", beerOnTheWall (n - 1) ++ "!"] | |
where | |
it = if n == 1 then "it" else "one" | |
beer 0 = "no more " ++ bottle 0 | |
beer x = unwords [show x, bottle x] | |
bottle 1 = "bottle" | |
bottle _ = "bottles" | |
beerOnTheWall n = beer n ++ " of beer on the wall" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment