Created
October 12, 2017 10:02
-
-
Save CarstenKoenig/fc18c0ed581d452fa93d92132c226621 to your computer and use it in GitHub Desktop.
MonadenAufgabe
This file contains 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
{-# 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