Hi all,<br><br>I wrote a simple interpreter that can be run in the console:<br><br><span style="font-family:courier new,monospace">data Interaction a b = Exit b<br> | Output b (Interaction a b)<br> | Input (a -> Interaction a b)</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">runConsole :: Interaction String String -> IO ()</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">runConsole (Exit b) =<br>
putStrLn $ "Finished. Result: " ++ b</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">runConsole (Output s cont) =<br> putStrLn s >> runConsole cont</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">runConsole (Input f) =<br>
putStr "> " >> getLine >>= runConsole . f</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">interpreter :: Int -> Interaction String String</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">interpreter i = interaction</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> where </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> interaction = Input input</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> input "exit" = Exit (show i)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> input "inc" = Output "ok" $ interpreter (i+1)</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> input "show" = Output (show i) interaction</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> input "hello"= Output "Hello World!" interaction</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> input s = Output ("Whas's '" ++ s ++ "' ?") interaction</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">main = runConsole .<br> Output "Known commands: show, inc, hello, exit" $ interpreter 5</span><br><br>I have not yet gained a good understanding of the continuation monad, but I wonder if it could be used here. What would a clean solution look like? Perhaps there are other things that need to be changed as well?<br>
<br>Regards,<br>Tim<br><br>