[Haskell-cafe] How to write fast for loops

John Lato jwlato at gmail.com
Sun Apr 27 00:13:52 UTC 2014


I usually use a manually written loop, but you can use Data.Vector for this
and it should fuse.

John L.
On Apr 26, 2014 2:41 PM, "Niklas Hambüchen" <mail at nh2.me> wrote:

> As you probably now, `forM_ [1..n]` is incredibly slow in Haskell due to
> lack of list fusion [1]; a manually written loop is 10x faster.
>
> How do you people work around this?
>
> I keep defining myself something like
>
>   loop :: (Monad m) => Int -> (Int -> m ()) -> m ()
>   loop bex f = go 0
>     where
>       go !n | n == bex  = return ()
>             | otherwise = f n >> go (n+1)
>
> Is there a function for this somewhere already?
> Or do you have another way to deal with this problem?
>
> Thanks!
>
>
> [1]: https://ghc.haskell.org/trac/ghc/ticket/8763
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140426/9e80f70e/attachment.html>


More information about the Haskell-Cafe mailing list