Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System.Exit
- import Control.Monad
- import Control.Monad.Free
- data TeletypeF next = Say String next | Ask (String -> next) | Stop
- instance Functor TeletypeF where
- fmap f (Say msg next) = Say msg (f next)
- fmap f (Ask k) = Ask (f . k)
- fmap _ Stop = Stop
- type Teletype = Free TeletypeF
- example :: Teletype ()
- example = Free $ Say "What is your name?" $ Free $ Ask $ \name ->
- if (length name >= 10) then Pure ()
- else Free $ Say ("Hello, " ++ name ++ "!") (Pure ())
- runIO :: Teletype a -> IO a
- runIO (Pure r) = return r
- runIO (Free (Say msg t)) = print msg >> runIO t
- runIO (Free (Ask f)) = getLine >>= runIO . f
- runIO (Free Stop) = exitSuccess
- main = runIO example
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement