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 .

Advertisements

October 21, 2011 - Posted by | Programming | , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: