Created
November 30, 2014 01:31
-
-
Save erantapaa/abadd351db3a87679607 to your computer and use it in GitHub Desktop.
Using StateT with Haskeline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- A receipe for using StateT with haskeline's InputT transformer. | |
-- Derived from this /r/haskell post: http://www.reddit.com/r/haskell/comments/1os0yq/haskeline_woes/ | |
-- | |
-- The key is using Control.Monad.State.Strict - using .Lazy doesn't work. | |
import Control.Monad.State.Strict | |
import Control.Monad.Trans (lift) | |
import System.Console.Haskeline | |
main = runStateT (runInputT defaultSettings loop) "" | |
check ma b fb = maybe b fb ma | |
loop = do | |
minput <- getInputLine "% " | |
check minput (return ()) $ \inp -> do | |
let args = words inp | |
case args of | |
("quit":_) -> do outputStrLn "quitting"; return () | |
("set":v:_) -> do outputStrLn $ "setting value to " ++ show v; lift $ put v; loop | |
("get":_) -> do v <- lift get; outputStrLn $ "the value is " ++ show v; loop | |
_ -> do outputStrLn "huh?"; loop | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment