<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!--Rendered using the Haskell Html Library v0.2--> <HTML ><HEAD ><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8" ><TITLE >Control.Monad.State.Lazy</TITLE ><LINK HREF="haddock.css" REL="stylesheet" TYPE="text/css" ><SCRIPT SRC="haddock-util.js" TYPE="text/javascript" ></SCRIPT ><SCRIPT TYPE="text/javascript" >window.onload = function () {setSynopsis("mini_Control-Monad-State-Lazy.html")};</SCRIPT ></HEAD ><BODY ><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD CLASS="topbar" ><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD ><IMG SRC="haskell_icon.gif" WIDTH="16" HEIGHT="16" ALT=" " ></TD ><TD CLASS="title" >mtl-1.1.0.2: Monad transformer library</TD ><TD CLASS="topbut" ><A HREF="index.html" >Contents</A ></TD ><TD CLASS="topbut" ><A HREF="doc-index.html" >Index</A ></TD ></TR ></TABLE ></TD ></TR ><TR ><TD CLASS="modulebar" ><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD ><FONT SIZE="6" >Control.Monad.State.Lazy</FONT ></TD ><TD ALIGN="right" ><TABLE CLASS="narrow" CELLSPACING="0" CELLPADDING="0" ><TR ><TD CLASS="infohead" >Portability</TD ><TD CLASS="infoval" >non-portable (multi-param classes, functional dependencies)</TD ></TR ><TR ><TD CLASS="infohead" >Stability</TD ><TD CLASS="infoval" >experimental</TD ></TR ><TR ><TD CLASS="infohead" >Maintainer</TD ><TD CLASS="infoval" >libraries@haskell.org</TD ></TR ></TABLE ></TD ></TR ></TABLE ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD ><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD CLASS="section4" ><B >Contents</B ></TD ></TR ><TR ><TD ><DL ><DT ><A HREF="#1" >The State Monad </A ></DT ><DT ><A HREF="#2" >The StateT Monad </A ></DT ><DT ><A HREF="#3" >Examples </A ></DT ></DL ></TD ></TR ></TABLE ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="section1" >Description</TD ></TR ><TR ><TD CLASS="doc" ><P >Lazy state monads. </P ><P >This module is inspired by the paper /Functional Programming with Overloading and Higher-Order Polymorphism/, Mark P Jones (<A HREF="http://web.cecs.pdx.edu/~mpj/" >http://web.cecs.pdx.edu/~mpj/</A >) Advanced School of Functional Programming, 1995. </P ><P >See below for examples. </P ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="section1" >Synopsis</TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="body" ><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD CLASS="decl" >module <A HREF="Control-Monad-State-Class.html" >Control.Monad.State.Class</A ></TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" ><SPAN CLASS="keyword" >newtype</SPAN > <A HREF="#t%3AState" >State</A > s a = <A HREF="#v%3AState" >State</A > {<TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD CLASS="recfield" ><A HREF="#v%3ArunState" >runState</A > :: s -> (a, s)</TD ></TR ></TABLE >}</TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" ><A HREF="#v%3AevalState" >evalState</A > :: <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s a -> s -> a</TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" ><A HREF="#v%3AexecState" >execState</A > :: <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s a -> s -> s</TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" ><A HREF="#v%3AmapState" >mapState</A > :: ((a, s) -> (b, s)) -> <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s a -> <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s b</TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" ><A HREF="#v%3AwithState" >withState</A > :: (s -> s) -> <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s a -> <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s a</TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" ><SPAN CLASS="keyword" >newtype</SPAN > <A HREF="#t%3AStateT" >StateT</A > s m a = <A HREF="#v%3AStateT" >StateT</A > {<TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD CLASS="recfield" ><A HREF="#v%3ArunStateT" >runStateT</A > :: s -> m (a, s)</TD ></TR ></TABLE >}</TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" ><A HREF="#v%3AevalStateT" >evalStateT</A > :: <A HREF="../base/Control-Monad.html#t%3AMonad" >Monad</A > m => <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m a -> s -> m a</TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" ><A HREF="#v%3AexecStateT" >execStateT</A > :: <A HREF="../base/Control-Monad.html#t%3AMonad" >Monad</A > m => <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m a -> s -> m s</TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" ><A HREF="#v%3AmapStateT" >mapStateT</A > :: (m (a, s) -> n (b, s)) -> <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m a -> <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s n b</TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" ><A HREF="#v%3AwithStateT" >withStateT</A > :: (s -> s) -> <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m a -> <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m a</TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" >module <A HREF="../base/Control-Monad.html" >Control.Monad</A ></TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" >module <A HREF="../base/Control-Monad-Fix.html" >Control.Monad.Fix</A ></TD ></TR ><TR ><TD CLASS="s8" ></TD ></TR ><TR ><TD CLASS="decl" >module <A HREF="Control-Monad-Trans.html" >Control.Monad.Trans</A ></TD ></TR ></TABLE ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="section1" >Documentation</TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" >module <A HREF="Control-Monad-State-Class.html" >Control.Monad.State.Class</A ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="section1" ><A NAME="1" ><A NAME="1" >The State Monad </A ></A ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" ><SPAN CLASS="keyword" >newtype</SPAN > <A NAME="t:State" ><A NAME="t%3AState" ></A ></A ><B >State</B > s a </TD ></TR ><TR ><TD CLASS="body" ><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD CLASS="ndoc" >A parameterizable state monad where <EM >s</EM > is the type of the state to carry and <EM >a</EM > is the type of the <EM >return value</EM >. </TD ></TR ><TR ><TD CLASS="section4" >Constructors</TD ></TR ><TR ><TD CLASS="body" ><TABLE CLASS="vanilla" CELLSPACING="5" CELLPADDING="0" ><TR ><TD CLASS="arg" ><A NAME="v:State" ><A NAME="v%3AState" ></A ></A ><B >State</B ></TD ><TD CLASS="rdoc" ></TD ></TR ><TR ><TD CLASS="body" COLSPAN="2" ><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0" ><TR ><TD CLASS="arg" ><A NAME="v:runState" ><A NAME="v%3ArunState" ></A ></A ><B >runState</B > :: s -> (a, s)</TD ><TD CLASS="rdoc" ></TD ></TR ></TABLE ></TD ></TR ></TABLE ></TD ></TR ><TR ><TD CLASS="section4" ><IMG SRC="minus.gif" CLASS="coll" ONCLICK="toggle(this,'i:State')" ALT="show/hide" > Instances</TD ></TR ><TR ><TD CLASS="body" ><DIV ID="i:State" STYLE="display:block;" ><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0" ><TR ><TD CLASS="decl" ><A HREF="Control-Monad-State-Class.html#t%3AMonadState" >MonadState</A > s (<A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="../base/Control-Monad.html#t%3AMonad" >Monad</A > (<A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="../base/Control-Monad.html#t%3AFunctor" >Functor</A > (<A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="../base/Control-Monad-Fix.html#t%3AMonadFix" >MonadFix</A > (<A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s)</TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" ><A NAME="v:evalState" ><A NAME="v%3AevalState" ></A ></A ><B >evalState</B ></TD ></TR ><TR ><TD CLASS="body" ><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD CLASS="arg" >:: </TD ><TD CLASS="rdoc" ></TD ></TR ><TR ><TD CLASS="arg" >=> <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s a</TD ><TD CLASS="rdoc" >The state to evaluate </TD ></TR ><TR ><TD CLASS="arg" >-> s</TD ><TD CLASS="rdoc" >An initial value </TD ></TR ><TR ><TD CLASS="arg" >-> a</TD ><TD CLASS="rdoc" >The return value of the state application </TD ></TR ><TR ><TD CLASS="ndoc" COLSPAN="2" >Evaluate this state monad with the given initial state,throwing away the final state. Very much like <TT >fst</TT > composed with <TT >runstate</TT >. </TD ></TR ></TABLE ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" ><A NAME="v:execState" ><A NAME="v%3AexecState" ></A ></A ><B >execState</B ></TD ></TR ><TR ><TD CLASS="body" ><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD CLASS="arg" >:: </TD ><TD CLASS="rdoc" ></TD ></TR ><TR ><TD CLASS="arg" >=> <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s a</TD ><TD CLASS="rdoc" >The state to evaluate </TD ></TR ><TR ><TD CLASS="arg" >-> s</TD ><TD CLASS="rdoc" >An initial value </TD ></TR ><TR ><TD CLASS="arg" >-> s</TD ><TD CLASS="rdoc" >The new state </TD ></TR ><TR ><TD CLASS="ndoc" COLSPAN="2" >Execute this state and return the new state, throwing away the return value. Very much like <TT >snd</TT > composed with <TT >runstate</TT >. </TD ></TR ></TABLE ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" ><A NAME="v:mapState" ><A NAME="v%3AmapState" ></A ></A ><B >mapState</B > :: ((a, s) -> (b, s)) -> <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s a -> <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s b</TD ></TR ><TR ><TD CLASS="doc" ><P >Map a stateful computation from one (return value, state) pair to another. For instance, to convert numberTree from a function that returns a tree to a function that returns the sum of the numbered tree (see the Examples section for numberTree and sumTree) you may write: </P ><PRE > sumNumberedTree :: (Eq a) => Tree a -> State (Table a) Int sumNumberedTree = mapState (\ (t, tab) -> (sumTree t, tab)) . numberTree </PRE ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" ><A NAME="v:withState" ><A NAME="v%3AwithState" ></A ></A ><B >withState</B > :: (s -> s) -> <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s a -> <A HREF="Control-Monad-State-Lazy.html#t%3AState" >State</A > s a</TD ></TR ><TR ><TD CLASS="doc" >Apply this function to this state and return the resulting state. </TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="section1" ><A NAME="2" ><A NAME="2" >The StateT Monad </A ></A ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" ><SPAN CLASS="keyword" >newtype</SPAN > <A NAME="t:StateT" ><A NAME="t%3AStateT" ></A ></A ><B >StateT</B > s m a </TD ></TR ><TR ><TD CLASS="body" ><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0" ><TR ><TD CLASS="ndoc" ><P >A parameterizable state monad for encapsulating an inner monad. </P ><P >The StateT Monad structure is parameterized over two things: </P ><UL ><LI > s - The state. </LI ><LI > m - The inner monad. </LI ></UL ><P >Here are some examples of use: </P ><P >(Parser from ParseLib with Hugs) </P ><PRE > type Parser a = StateT String [] a ==> StateT (String -> [(a,String)]) </PRE ><P >For example, item can be written as: </P ><PRE > item = do (x:xs) <- get put xs return x type BoringState s a = StateT s Indentity a ==> StateT (s -> Identity (a,s)) type StateWithIO s a = StateT s IO a ==> StateT (s -> IO (a,s)) type StateWithErr s a = StateT s Maybe a ==> StateT (s -> Maybe (a,s)) </PRE ></TD ></TR ><TR ><TD CLASS="section4" >Constructors</TD ></TR ><TR ><TD CLASS="body" ><TABLE CLASS="vanilla" CELLSPACING="5" CELLPADDING="0" ><TR ><TD CLASS="arg" ><A NAME="v:StateT" ><A NAME="v%3AStateT" ></A ></A ><B >StateT</B ></TD ><TD CLASS="rdoc" ></TD ></TR ><TR ><TD CLASS="body" COLSPAN="2" ><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0" ><TR ><TD CLASS="arg" ><A NAME="v:runStateT" ><A NAME="v%3ArunStateT" ></A ></A ><B >runStateT</B > :: s -> m (a, s)</TD ><TD CLASS="rdoc" ></TD ></TR ></TABLE ></TD ></TR ></TABLE ></TD ></TR ><TR ><TD CLASS="section4" ><IMG SRC="minus.gif" CLASS="coll" ONCLICK="toggle(this,'i:StateT')" ALT="show/hide" > Instances</TD ></TR ><TR ><TD CLASS="body" ><DIV ID="i:StateT" STYLE="display:block;" ><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0" ><TR ><TD CLASS="decl" ><A HREF="Control-Monad-Writer-Class.html#t%3AMonadWriter" >MonadWriter</A > w m => <A HREF="Control-Monad-Writer-Class.html#t%3AMonadWriter" >MonadWriter</A > w (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="../base/Control-Monad.html#t%3AMonad" >Monad</A > m => <A HREF="Control-Monad-State-Class.html#t%3AMonadState" >MonadState</A > s (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="Control-Monad-Reader-Class.html#t%3AMonadReader" >MonadReader</A > r m => <A HREF="Control-Monad-Reader-Class.html#t%3AMonadReader" >MonadReader</A > r (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="Control-Monad-Error-Class.html#t%3AMonadError" >MonadError</A > e m => <A HREF="Control-Monad-Error-Class.html#t%3AMonadError" >MonadError</A > e (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="Control-Monad-Trans.html#t%3AMonadTrans" >MonadTrans</A > (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="../base/Control-Monad.html#t%3AMonad" >Monad</A > m => <A HREF="../base/Control-Monad.html#t%3AMonad" >Monad</A > (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="../base/Control-Monad.html#t%3AMonad" >Monad</A > m => <A HREF="../base/Control-Monad.html#t%3AFunctor" >Functor</A > (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="../base/Control-Monad-Fix.html#t%3AMonadFix" >MonadFix</A > m => <A HREF="../base/Control-Monad-Fix.html#t%3AMonadFix" >MonadFix</A > (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="../base/Control-Monad.html#t%3AMonadPlus" >MonadPlus</A > m => <A HREF="../base/Control-Monad.html#t%3AMonadPlus" >MonadPlus</A > (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="Control-Monad-Trans.html#t%3AMonadIO" >MonadIO</A > m => <A HREF="Control-Monad-Trans.html#t%3AMonadIO" >MonadIO</A > (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m)</TD ></TR ><TR ><TD CLASS="decl" ><A HREF="Control-Monad-Cont-Class.html#t%3AMonadCont" >MonadCont</A > m => <A HREF="Control-Monad-Cont-Class.html#t%3AMonadCont" >MonadCont</A > (<A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m)</TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" ><A NAME="v:evalStateT" ><A NAME="v%3AevalStateT" ></A ></A ><B >evalStateT</B > :: <A HREF="../base/Control-Monad.html#t%3AMonad" >Monad</A > m => <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m a -> s -> m a</TD ></TR ><TR ><TD CLASS="doc" >Similar to <TT ><A HREF="Control-Monad-State-Lazy.html#v%3AevalState" >evalState</A ></TT > </TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" ><A NAME="v:execStateT" ><A NAME="v%3AexecStateT" ></A ></A ><B >execStateT</B > :: <A HREF="../base/Control-Monad.html#t%3AMonad" >Monad</A > m => <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m a -> s -> m s</TD ></TR ><TR ><TD CLASS="doc" >Similar to <TT ><A HREF="Control-Monad-State-Lazy.html#v%3AexecState" >execState</A ></TT > </TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" ><A NAME="v:mapStateT" ><A NAME="v%3AmapStateT" ></A ></A ><B >mapStateT</B > :: (m (a, s) -> n (b, s)) -> <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m a -> <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s n b</TD ></TR ><TR ><TD CLASS="doc" >Similar to <TT ><A HREF="Control-Monad-State-Lazy.html#v%3AmapState" >mapState</A ></TT > </TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" ><A NAME="v:withStateT" ><A NAME="v%3AwithStateT" ></A ></A ><B >withStateT</B > :: (s -> s) -> <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m a -> <A HREF="Control-Monad-State-Lazy.html#t%3AStateT" >StateT</A > s m a</TD ></TR ><TR ><TD CLASS="doc" >Similar to <TT ><A HREF="Control-Monad-State-Lazy.html#v%3AwithState" >withState</A ></TT > </TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" >module <A HREF="../base/Control-Monad.html" >Control.Monad</A ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" >module <A HREF="../base/Control-Monad-Fix.html" >Control.Monad.Fix</A ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="decl" >module <A HREF="Control-Monad-Trans.html" >Control.Monad.Trans</A ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="section1" ><A NAME="3" ><A NAME="3" >Examples </A ></A ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="doc" ><P >A function to increment a counter. Taken from the paper <EM >Generalising Monads to Arrows</EM >, John Hughes (<A HREF="http://www.math.chalmers.se/~rjmh/" >http://www.math.chalmers.se/~rjmh/</A >), November 1998: </P ><PRE > tick :: State Int Int tick = do n <- get put (n+1) return n </PRE ><P >Add one to the given number using the state monad: </P ><PRE > plusOne :: Int -> Int plusOne n = execState tick n </PRE ><P >A contrived addition example. Works only with positive numbers: </P ><PRE > plus :: Int -> Int -> Int plus n x = execState (sequence $ replicate n tick) x </PRE ><P >An example from <EM >The Craft of Functional Programming</EM >, Simon Thompson (<A HREF="http://www.cs.kent.ac.uk/people/staff/sjt/" >http://www.cs.kent.ac.uk/people/staff/sjt/</A >), Addison-Wesley 1999: "Given an arbitrary tree, transform it to a tree of integers in which the original elements are replaced by natural numbers, starting from 0. The same element has to be replaced by the same number at every occurrence, and when we meet an as-yet-unvisited element we have to find a 'new' number to match it with:" </P ><PRE > data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Eq) type Table a = [a] </PRE ><PRE > numberTree :: Eq a => Tree a -> State (Table a) (Tree Int) numberTree Nil = return Nil numberTree (Node x t1 t2) = do num <- numberNode x nt1 <- numberTree t1 nt2 <- numberTree t2 return (Node num nt1 nt2) where numberNode :: Eq a => a -> State (Table a) Int numberNode x = do table <- get (newTable, newPos) <- return (nNode x table) put newTable return newPos nNode:: (Eq a) => a -> Table a -> (Table a, Int) nNode x table = case (findIndexInList (== x) table) of Nothing -> (table ++ [x], length table) Just i -> (table, i) findIndexInList :: (a -> Bool) -> [a] -> Maybe Int findIndexInList = findIndexInListHelp 0 findIndexInListHelp _ _ [] = Nothing findIndexInListHelp count f (h:t) = if (f h) then Just count else findIndexInListHelp (count+1) f t </PRE ><P >numTree applies numberTree with an initial state: </P ><PRE > numTree :: (Eq a) => Tree a -> Tree Int numTree t = evalState (numberTree t) [] </PRE ><PRE > testTree = Node "Zero" (Node "One" (Node "Two" Nil Nil) (Node "One" (Node "Zero" Nil Nil) Nil)) Nil numTree testTree => Node 0 (Node 1 (Node 2 Nil Nil) (Node 1 (Node 0 Nil Nil) Nil)) Nil </PRE ><P >sumTree is a little helper function that does not use the State monad: </P ><PRE > sumTree :: (Num a) => Tree a -> a sumTree Nil = 0 sumTree (Node e t1 t2) = e + (sumTree t1) + (sumTree t2) </PRE ></TD ></TR ><TR ><TD CLASS="s15" ></TD ></TR ><TR ><TD CLASS="botbar" >Produced by <A HREF="http://www.haskell.org/haddock/" >Haddock</A > version 2.4.2</TD ></TR ></TABLE ></BODY ></HTML >