Execute HQ9+/Haskell: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with '<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" …')
 
m (Changed spacing.)
Line 22: Line 22:
up (x : xs) = Char.toUpper x : xs
up (x : xs) = Char.toUpper x : xs
num (-1) = "99"
num (-1) = "99"
num 0 = "no more"
num 0 = "no more"
num n = show n
num n = show n
s 1 = ""
s 1 = ""
s _ = "s"</lang>
s _ = "s"</lang>

Revision as of 20:15, 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) = Char.toUpper x : xs
       num (-1) = "99"
       num 0    = "no more"
       num n    = show n
       s 1 = ""
       s _ = "s"</lang>