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"><<a href="mailto:crazyender@gmail.com">crazyender@gmail.com</a>></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 <<a href="mailto:lemming@henning-thielemann.de">lemming@henning-thielemann.de</a>>:<br>
<div><div></div><div class="h5">><br>
> On Sun, 19 Dec 2010, ender wrote:<br>
><br>
>>> do<br>
>>> alloca $ \value -> do<br>
>>> poke value (500000::Int)<br>
>>> allocaArray 4 $ \part_stack -> do<br>
>>> alloca $ \part_ptr -> do<br>
>>> poke part_ptr part_stack<br>
>>> let loop = do<br>
>>> val <- peek value<br>
>>> if val == 0 then return () else do<br>
>>> p <- peek part_ptr<br>
>>> poke p (val `rem` 10000)<br>
>>> poke part_ptr (p `plusPtr` 1)<br>
>>> poke value (val `quot` 10000)<br>
>>> loop<br>
>>> loop<br>
>><br>
>> and I really think that's not a "haskell way", it's just translate c<br>
>> code into haskell code byte by byte<br>
>> My question is: how to translate above c code into haskell in "haskell<br>
>> way"<br>
><br>
> If the count of loop runs does not depend on results of the loop body, then<br>
> 'mapM' and 'mapM_' applied to the list of increasing pointers are your<br>
> friends. In your case, the loop aborts when 'val' becomes zero. I'm<br>
> certainly thinking too complicated, but you might use MaybeT IO () (e.g.<br>
> from transformers package) and abort 'mapM_' with 'mzero' when 'val' becomes<br>
> zero. (MaybeT IO a) is like an IO monad with an early exit (somehow an<br>
> exception) option.<br>
><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>