Execute HQ9+/Haskell: Difference between revisions

m
Fixed syntax highlighting.
m (Added common page structure)
m (Fixed syntax highlighting.)
 
(3 intermediate revisions by 3 users not shown)
Line 1:
{{implementation|HQ9+}}{{collection|RCHQ9+}}
This [[HQ9+]] interpreter is written in [[Haskell]].
We use [https://hackage.haskell.org/package/base-4.11.1.0/docs/Data-Traversable.html#v:mapAccumR mapAccumR] to maintain the accumulator.
<lang haskell>import Char (toLower, toUpper)
However, the specification doesn't say what to do with said accumulator on completion.
<syntaxhighlight lang="haskell">module Main where
 
<lang haskell>import Data.Char (toLower, toUpper)
main = interact hq9p
import Data.List -- for concat
import Data.Traversable -- for mapAccumR
 
hq9pmain :: StringIO -> String()
main = do
hq9p source = concatMap run $ map toLower source
s <- getLine
where run 'h' = "Hello, world!\n"
putStrLn (snd (hq9p s))
run 'q' = source
 
run '9' = bottles
hq9p :: String -> (Int,String)
run _ = ""
hq9p sourcesrc = concatMapfin $ mapAccumR run 0 $ map toLower sourcesrc
-- Obviously, the final case works just fine for '+'.
where
fin (acc,log) = (acc,concat log)
run acc ch = case ch of
where run 'h' =-> (acc,"Hello, world!\n")
'q' -> (acc,src)
run '9' =-> (acc,bottles)
'+' -> s(acc + 1 = ,"")
run '_' -> = (acc,"")
 
bottles :: String
bottles = concat[99, 98 .. 0] >>= beers
where
[up (bob n) ++ wall ++ ", " ++ bob n ++ ".\n" ++
beers n = unlines [
pass n ++ bob (n - 1) ++ wall ++ ".\n\n" |
bob n <-++ [99" on the wall, 98" ..++ 0]]bob n ++ ".",
where bob n = numpass n ++ "bob bottle"(n ++- s n1) ++ " ofon beerthe wall.\n"]
bob walln = " on the wall"
let nu pass= 0case =n "Goof to{ the(-1) store-> and"99"; buy0 some-> more,"No more"; n -> show n; }
pass _ s = "Takeif onen down== and1 passthen it"" around,else "s"
in nu up++ (x" :bottle" xs)++ =s toUpper++ x" :of xsbeer"
pass n = case num (-1) =n "99"of
0 -> num"Go 0to the store and =buy "nosome more, "
_ -> num"Take none down and pass =it showaround, n"</syntaxhighlight>
s 1 = ""
s _ = "s"</lang>
9,476

edits