Execute HQ9+/Haskell: Difference between revisions
Content added Content deleted
Underscore (talk | contribs) m (Changed spacing.) |
Underscore (talk | contribs) m (Whoops, we don't need that qualifier.) |
||
Line 20: | Line 20: | ||
pass 0 = "Go to the store and buy some more, " |
pass 0 = "Go to the store and buy some more, " |
||
pass _ = "Take one down and pass it around, " |
pass _ = "Take one down and pass it around, " |
||
up (x : xs) = |
up (x : xs) = toUpper x : xs |
||
num (-1) = "99" |
num (-1) = "99" |
||
num 0 = "no more" |
num 0 = "no more" |
Revision as of 20:19, 18 December 2009
<lang haskell>import Char (toLower, toUpper)
main = interact hq9p
hq9p :: String -> String hq9p source = concatMap run $ map toLower source
where run 'h' = "Hello, world!\n" run 'q' = source run '9' = bottles run _ = "" -- Obviously, the final case works just fine for '+'.
bottles :: String bottles = 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) = toUpper x : xs num (-1) = "99" num 0 = "no more" num n = show n s 1 = "" s _ = "s"</lang>