Recursion replaces loops.  If it needs to be monadic or not depends on what you want to do.<br><br><div class="gmail_quote">On Sun, Dec 19, 2010 at 10:53 AM, ender <span dir="ltr">&lt;<a href="mailto:crazyender@gmail.com">crazyender@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">2010/12/19 Henning Thielemann &lt;<a href="mailto:lemming@henning-thielemann.de">lemming@henning-thielemann.de</a>&gt;:<br>

<div><div></div><div class="h5">&gt;<br>
&gt; On Sun, 19 Dec 2010, ender wrote:<br>
&gt;<br>
&gt;&gt;&gt;  do<br>
&gt;&gt;&gt;  alloca $ \value -&gt; do<br>
&gt;&gt;&gt;  poke value (500000::Int)<br>
&gt;&gt;&gt;  allocaArray 4 $ \part_stack -&gt; do<br>
&gt;&gt;&gt;  alloca $ \part_ptr -&gt; do<br>
&gt;&gt;&gt;  poke part_ptr part_stack<br>
&gt;&gt;&gt;  let loop = do<br>
&gt;&gt;&gt;           val &lt;- peek value<br>
&gt;&gt;&gt;           if val == 0 then return () else do<br>
&gt;&gt;&gt;           p &lt;- peek part_ptr<br>
&gt;&gt;&gt;           poke p (val `rem` 10000)<br>
&gt;&gt;&gt;           poke part_ptr (p `plusPtr` 1)<br>
&gt;&gt;&gt;           poke value (val `quot` 10000)<br>
&gt;&gt;&gt;           loop<br>
&gt;&gt;&gt;  loop<br>
&gt;&gt;<br>
&gt;&gt; and I really think that&#39;s not a &quot;haskell way&quot;, it&#39;s just translate c<br>
&gt;&gt; code into haskell code byte by byte<br>
&gt;&gt; My question is: how to translate above c code into haskell in &quot;haskell<br>
&gt;&gt; way&quot;<br>
&gt;<br>
&gt; If the count of loop runs does not depend on results of the loop body, then<br>
&gt; &#39;mapM&#39; and &#39;mapM_&#39; applied to the list of increasing pointers are your<br>
&gt; friends. In your case, the loop aborts when &#39;val&#39; becomes zero. I&#39;m<br>
&gt; certainly thinking too complicated, but you might use MaybeT IO () (e.g.<br>
&gt; from transformers package) and abort &#39;mapM_&#39; with &#39;mzero&#39; when &#39;val&#39; becomes<br>
&gt; zero. (MaybeT IO a) is like an IO monad with an early exit (somehow an<br>
&gt; exception) option.<br>
&gt;<br>
</div></div>Hi Henning:<br>
   Thanks for your quick reply. So recursive and monad is the proper<br>
way to simulate loop,right?<br>
<br>
Thanks and BR<br>
<div><div></div><div class="h5"><br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br>