Skip to content

Instantly share code, notes, and snippets.

@parsonsmatt
Last active March 7, 2017 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsonsmatt/3c030058e5bfb19637c268806bc51c38 to your computer and use it in GitHub Desktop.
Save parsonsmatt/3c030058e5bfb19637c268806bc51c38 to your computer and use it in GitHub Desktop.
26 Bottles of Functional Programming
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