Skip to content

Instantly share code, notes, and snippets.

@erantapaa
Created November 30, 2014 01:31
Show Gist options
  • Save erantapaa/abadd351db3a87679607 to your computer and use it in GitHub Desktop.
Save erantapaa/abadd351db3a87679607 to your computer and use it in GitHub Desktop.
Using StateT with Haskeline
-- 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