My Weblog

Blog about programming and math

Monad Transformer

Monad transformer is way to compose monads simultaneously.Computation of gcd using state monad but what if we want to print value of a and b for each call. Clearly we are looking to compose state monad and IO monad and our candidate for this purpose is monad transformer.

import Control.Monad.Identity
import Control.Monad.State

gcD :: StateT  ( Integer , Integer )  IO Integer        
gcD = get >>=( \( a , b ) -> case b ==0  of
                              True -> ( liftIO $ print ( a , b ) ) >> return a
                              _    -> ( liftIO $ print ( a , b ) ) >> put ( b , mod a b ) >> gcD )



main = runStateT gcD  (123 ,22)

PS.This is simple example and you can see more complex example on real world haskell .

October 21, 2011 Posted by | Programming | , , | Leave a Comment

   

Follow

Get every new post delivered to your Inbox.

Join 197 other followers