How to resolve the algorithm 99 bottles of beer step by step in the Haskell programming language
How to resolve the algorithm 99 bottles of beer step by step in the Haskell programming language
Table of Contents
Problem Statement
Display the complete lyrics for the song: 99 Bottles of Beer on the Wall.
The lyrics follow this form: ... and so on, until reaching 0 (zero). Grammatical support for 1 bottle of beer is optional. As with any puzzle, try to do it in as creative/concise/comical a way as possible (simple, obvious solutions allowed, too).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 99 bottles of beer step by step in the Haskell programming language
First example:
This code snippet is written in Haskell and it implements the "99 bottles of beer" song.
It starts by defining the main
function which is the entry point of the program.
The main
function calls the mapM_
function which applies the beer
function to each element of the list [99, 98 .. 0]
and then prints the result.
The beer
function takes a number as input and returns a string representing the corresponding verse of the song.
If the number is 1, the function returns the first verse of the song, if the number is 0, the function returns the last verse of the song, otherwise the function returns the verse for the given number.
Second example:
This code snippet is a Haskell implementation of the "99 bottles of beer" song using Template Haskell.
The code starts by defining the main
function which is the entry point of the program.
The main
function calls the putStr
function which prints the value of the songString
variable.
The songString
variable is defined using Template Haskell and it contains the lyrics of the song.
The song
variable is defined by calling the execWriter
function with the mapM_
function which applies the verse
function to each element of the list [99,98..1]
.
The verse
function takes a number as input and returns a string representing the corresponding verse of the song.
Third example:
This code snippet is a Haskell implementation of the "99 bottles of beer" song using point-free style.
The code starts by defining the main
function which is the entry point of the program.
The main
function calls the putStrLn
function which prints the value of the expression unlines (mapM_ incantation [99, 98 .. 0])
.
The incantation
function takes a number as input and returns a string representing the corresponding verse of the song.
The inventory
function takes a number as input and returns a string representing the number of bottles of beer.
The asset
function takes a number as input and returns a string representing the number of bottles of beer.
The location
variable is a string representing the location of the bottles of beer.
The distribution
variable is a string representing the distribution of the bottles of beer.
The solution
variable is a string representing the solution to the song.
Source code in the haskell programming language
main = mapM_ (putStrLn . beer) [99, 98 .. 0]
beer 1 = "1 bottle of beer on the wall\n1 bottle of beer\nTake one down, pass it around"
beer 0 = "better go to the store and buy some more."
beer v = show v ++ " bottles of beer on the wall\n"
++ show v
++" bottles of beer\nTake one down, pass it around\n"
++ head (lines $ beer $ v-1) ++ "\n"
import qualified Char
main = putStr $ concat
[up (bob n) ++ wall ++ ", " ++ bob n ++ ".\n" ++
pass n ++ bob (n - 1) ++ wall ++ ".\n\n" |
n <- [99, 98 .. 0]]
where bob n = (num n) ++ " bottle" ++ (s n) ++ " of beer"
wall = " on the wall"
pass 0 = "Go to the store and buy some more, "
pass _ = "Take one down and pass it around, "
up (x : xs) = Char.toUpper x : xs
num (-1) = "99"
num 0 = "no more"
num n = show n
s 1 = ""
s _ = "s"
{-# LANGUAGE TemplateHaskell #-}
-- build with "ghc --make beer.hs"
module Main where
import Language.Haskell.TH
import Control.Monad.Writer
-- This is calculated at compile time, and is equivalent to
-- songString = "99 bottles of beer on the wall\n99 bottles..."
songString =
$(let
sing = tell -- we can't sing very well...
someBottles 1 = "1 bottle of beer "
someBottles n = show n ++ " bottles of beer "
bottlesOfBeer n = (someBottles n ++)
verse n = do
sing $ n `bottlesOfBeer` "on the wall\n"
sing $ n `bottlesOfBeer` "\n"
sing $ "Take one down, pass it around\n"
sing $ (n - 1) `bottlesOfBeer` "on the wall\n\n"
song = execWriter $ mapM_ verse [99,98..1]
in return $ LitE $ StringL $ song)
main = putStr songString
location, distribution, solution :: String
[location, distribution, solution] =
[ "on the wall",
"Take one down, pass it around",
"Better go to the store to buy some more"
]
incantation :: Int -> String
incantation n
| 0 == n = solution
| otherwise =
unlines
[ inventory n,
asset n,
distribution,
inventory $ pred n
]
inventory :: Int -> String
inventory = unwords . (: [location]) . asset
asset :: Int -> String
asset n =
let suffix n
| 1 == n = []
| otherwise = ['s']
in unwords
[show n, (reverse . concat) $ suffix n : ["elttob"]]
main :: IO ()
main = putStrLn $ unlines (incantation <$> [99, 98 .. 0])
You may also check:How to resolve the algorithm Draw a pixel step by step in the Tcl programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Scratch programming language
You may also check:How to resolve the algorithm Sockets step by step in the D programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the min programming language