I&#39;m trying to understand how to use State in a function. I&#39;ve avoided the topic for several years because State just never seemed very useful, but I figure it&#39;s time for me to figure it out:<div><br></div><div>

I have the following function</div><div><br></div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt; update :: (a -&gt; (r,a)) -&gt; Int -&gt; [a] -&gt; (r, [a])</span></font></div>

<div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt; update s 0 (a:as) = let (r,a&#39;) = s a in (r,a&#39;:as)</span></font></div>
<div>
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt; update s i (a:as) = let (r,as&#39;) = update s (i-1) as in (r, a:as&#39;)</span></font></div>

<div><br></div><div>which updates a particular element of a list. Looking at it, I see two parts of the type signature that look like State types, which leads me to think of this:</div><div><br></div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">&gt; update&#39; :: State a r -&gt; Int -&gt; State [a] r</font></div>

<div><br></div><div>Which leads to me writing this:</div><div><br></div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt; update&#39; s 0 = do</span></font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt;    (a:as) &lt;- get</span></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt;    let (r, a&#39;) = runState s a</span></font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt;    put (a&#39;:as)</span></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt;    return r</span></font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt; update&#39; s i = do</span></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt;    (a:as) &lt;- get</span></font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt;    put as</span></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt;    r &lt;- update&#39; s (i-1)</span></font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt;    as&#39; &lt;- get</span></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt;    put (a:as&#39;)</span></font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">&gt;    return r</span></font></div><div><br></div><div>Now, this just looks awful. The first half, the base condition, is actually &quot;running&quot; a State calculation. And the second half sets the state within the monad twice!</div>

<div><br></div><div>I like the idea of using State because it simplifies the type. When I see (a -&gt; (b,a)) I say &quot;Wait a second, that&#39;s a State calculation, isn&#39;t it?&quot; and then, hopefully, generalize. But I can&#39;t write that calculation nearly as concisely. How do I do this properly?</div>

</div></div></div></div>