Skip to content

Instantly share code, notes, and snippets.

@brase
Forked from CarstenKoenig/Monaden.hs
Created October 12, 2017 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brase/02d5635552ea72c4b3ac97a5b5dfe177 to your computer and use it in GitHub Desktop.
Save brase/02d5635552ea72c4b3ac97a5b5dfe177 to your computer and use it in GitHub Desktop.
MonadenAufgabe
{-# LANGUAGE InstanceSigs #-}
module ReaderM where
import Control.Monad.Reader
import Control.Monad.State.Strict
import Data.Functor.Identity
data Config = Config { nr :: Int }
main :: IO ()
main = do
let config = Config 42
runReaderT (verarbeitung "Hallo") config
useConfig :: ReaderT Config IO ()
useConfig = do
configNr <- asks nr
liftIO $ print configNr
verarbeitung :: String -> ReaderT Config IO ()
verarbeitung text = do
useConfig
liftIO $ putStrLn text
----------------------------------------------------------------------
data MyReader r a = MyReader (r -> a)
runMyReader :: MyReader r a -> r -> a
runMyReader (MyReader reader) r = reader r
myAsk :: MyReader r r
myAsk = MyReader id
myAsks :: (r -> b) -> MyReader r b
myAsks f = MyReader f
instance Functor (MyReader r) where
fmap f (MyReader g) = MyReader (f.g)
instance Applicative (MyReader r) where
pure a = MyReader $ const a
(MyReader rf) <*> (MyReader ra) =
MyReader (\r -> (rf r) (ra r))
instance Monad (MyReader r) where
(>>=) :: MyReader r a -> (a -> MyReader r b) -> MyReader r b
(>>=) (MyReader ra) fab =
MyReader (\r ->
let (MyReader rb) = fab (ra r)
in rb r)
plusReader :: MyReader Int Int
plusReader = do
n <- myAsk
return $ n+1
----------------------------------------------------------------------
beispiel :: Int -> State Int Int
beispiel a = do
zahl <- get
put $ zahl + a
return zahl
data MyState s a = MyState { runMyState :: s -> (a, s) }
evalMyState :: MyState s a -> s -> a
evalMyState ma initS = fst $ runMyState ma initS
myGet :: MyState s s
myGet = undefined
myPut :: s -> MyState s ()
myPut = undefined
instance Functor (MyState s) where
instance Applicative (MyState s) where
instance Monad (MyState s) where
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment