Download code

Jump to: navigation, search

Back to 99_Bottles_of_Beer_(Haskell)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

beer99.hs

 1 {- The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/99_Bottles_of_Beer_(Haskell)?oldid=16945
14 -}
15 
16 bottles :: Integer -> [String]
17 bottles n | n > 0     = countdown n
18           | otherwise = error "Not even a single bottle of beer?  You must have a drinking problem..."
19   where countdown :: Integer -> [String]
20 	countdown n = do i <- [n,n-1..0]
21 	                 let b    = bottleCount i
22 	                     b1   = bottleCount (i-1)
23 	                     next = nextAction i
24 	                 [(b ++ " on the wall, " ++ b ++ "."),
25 	                  (next ++ ", " ++ b1 ++ " on the wall."),
26 	                  ""]
27 
28         bottleCount 0 = "No more bottles of beer"
29 	bottleCount 1 = "1 bottle of beer"
30 	bottleCount n | n > 0 = show n ++ " bottles of beer"
31 	bottleCount _ = show n ++ " bottles of beer"
32         nextAction 0 = "Go to the store, buy some more"
33 	nextAction _ = "Take one down, pass it around"
34 main :: IO ()
35 main = mapM_ putStrLn (bottles 99)


hijacker
hijacker
hijacker
hijacker