Execute HQ9+/Haskell: Difference between revisions

From Rosetta Code
Content added Content deleted
(does not work fine for '+')
Line 1: Line 1:
{{implementation|HQ9+}}{{collection|RCHQ9+}}
{{implementation|HQ9+}}{{collection|RCHQ9+}}
This [[HQ9+]] interpreter is written in [[Haskell]].
This [[HQ9+]] interpreter is written in [[Haskell]].
{{incomplete|haskell|Command '+' is treated incorrectly. Implementing the accumulator in Haskell is not trivial because it has no variables.}}
<lang haskell>import Char (toLower, toUpper)
<lang haskell>import Char (toLower, toUpper)



Revision as of 10:44, 4 January 2017

Execute HQ9+/Haskell is an implementation of HQ9+. Other implementations of HQ9+.
Execute HQ9+/Haskell is part of RCHQ9+. You may find other members of RCHQ9+ at Category:RCHQ9+.

This HQ9+ interpreter is written in Haskell.

This example is incomplete. Command '+' is treated incorrectly. Implementing the accumulator in Haskell is not trivial because it has no variables. Please ensure that it meets all task requirements and remove this message.

<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>