<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>You can simply use a state monad and then extract the data later
when you don't want the extra state, viz.<br>
</p>
<pre><code class="haskell hljs"><p><span class="hljs-title">sum</span> :: (<span class="hljs-type">Num</span> a) => [a] -> a
<span class="hljs-title">sum</span> xs = flip execState <span class="hljs-number">0</span> $
mapM_ (\x -> <span class="hljs-keyword">do</span> { s <- get ; put $ x + s }) xs</p></code></pre>
<br>
> For instance I often keep a pseudorandom generator as the
state, and use exception handling.<br>
<br>
I'd use a monad transformer. Either `ExceptT` (which would add
exceptions to the state monad) or `StateT` (which would add state to
an exception monad). <br>
<br>
<div class="moz-cite-prefix">On 05/15/2018 12:27 AM, Dennis Raddle
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAKxLvorL9hVj-Hw45aYPL00KKhe9gKSrBW_eDVmfW+fG8AaCrw@mail.gmail.com">
<div dir="ltr">
<div>I find myself using the state and exception monads at large
scale in my programs. For instance I often keep a pseudorandom
generator as the state, and use exception handling.<br>
</div>
<div><br>
</div>
<div>In local computations, I may want to use some additional
state. Is there a way to add a little extra state temporarily?</div>
<div><br>
</div>
<div>Let's say I have <br>
</div>
<div><br>
</div>
<div>data State1 = State1 StdGen</div>
<div><br>
</div>
<div>type M1 = State State1</div>
<div><br>
</div>
<div>data State2 = State2 State1 Int</div>
<div><br>
</div>
<div>type M2 = State State2</div>
<div><br>
</div>
<div>runM2 :: M2 a -> Int -> M1 a<br>
</div>
<div><br>
</div>
<div>someFunc :: M1 Double<br>
</div>
<div>someFunc = do</div>
<div> r <- <compute something pseudorandomly></div>
<div> r2 <- runM2 (deeperFunc r) 3</div>
<div> <..etc..><br>
</div>
<div><br>
</div>
<div>deeperFunc :: Double -> M2 Double</div>
<div><br>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
<a class="moz-txt-link-freetext" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a>
Only members subscribed via the mailman list are allowed to post.</pre>
</blockquote>
<br>
<div class="moz-signature">-- <br>
<br>
<br>
<br>
<strong
style="color:#000;font-family:Helvetica,Arial;font-size:116%;">Vanessa
McHale</strong><br>
<span
style="color:#000;font-family:Helvetica,Arial;font-size:100%;"><span
style="color:#f00">Functional Compiler Engineer |</span>
Chicago, IL</span>
<br>
<br>
<span
style="color:#999;font-family:Helvetica,Arial;font-size:85%;">Website:
<a href="http://iohk.io" style="color:#f00;">www.iohk.io</a></span><br>
<span
style="color:#999;font-family:Helvetica,Arial;font-size:85%;">Twitter:
@vamchale</span><br>
<span
style="color:#999;font-family:Helvetica,Arial;font-size:85%;">PGP
Key ID: 4209B7B5</span><br>
<br>
<a href="http://iohk.io"><img
src="https://static.iohk.io/logo/logo-email.png" alt="Input
Output" height="46" width="200"></a>
<br>
<br>
<a href="https://twitter.com/InputOutputHK"><img
src="https://static.iohk.io/images/ic-tw.png" alt="Twitter"
height="16" width="18"></a>
<a href="https://github.com/input-output-hk"><img
src="https://static.iohk.io/images/ic-gh.png" alt="Github"
height="16" width="21"></a>
<a href="https://www.linkedin.com/company/input-output-global"><img
src="https://static.iohk.io/images/ic-li.png" alt="LinkedIn"
height="16" width="19"></a>
<br>
</div>
</body>
</html>