Execute HQ9+: Difference between revisions

Console output is made using monads
(Added an Agena sample)
(Console output is made using monads)
Line 720:
| be '+' = eval' xs (a+1)
| else = fail "Invalid instruction."
where be c = char.upper x == c
h = "Hello, world!"
q = src
Line 731:
++ bottles xs</lang>
 
It slightly alters an original HQ9+ specification. HQ9+ is an impure language that does console output. However console output is the only interaction that a user can see when executing HQ9+ program. This interpreter doesn't output to console but instead generates a list with all outputs. An accumulator is moved to the interpter arguments and the need for a reference cell is eliminated. Once an interpreter completes a client code can output to console using monads like so:
 
<lang ela>open console imperative monad io
 
each writen <| eval "HQ9+"</lang>
print_and_eval src = do
res <- return $ eval src
return $ each print res
where print x = do putStrLn x
 
print_and_eval "HQ9+" ::: IO</lang>
 
=={{header|Erlang}}==
Anonymous user