[Haskell-cafe] Optimizing 'sequence'

Antoine Latter aslatter at gmail.com
Mon Jul 21 15:20:02 EDT 2008


On Mon, Jul 21, 2008 at 1:54 PM, Gracjan Polak <gracjanpolak at gmail.com> wrote:
>
> Hi all,
>
> On the other day I noticed that we could optimize 'sequence' more.
> I needed it for my monadic parser. Below is my small experiment.
> Sequence from standard library needs 2.3s to finish (and additional
> stack space), my version uses only 0.65s and default stack.
>
> Is my version better or am I missing something obvious?
>

How does your version compare with the library version in the following tests:

test1 = do
   (x:_) <- sequence [return 5, undefined]
   return x

test2 = do
   (x:_) <- sequence $ return 5 : undefined
   return x

main = do
   print $ runIdentity test1
   print $ runIdentity test2


The function "runIdentity" is found in Control.Monad.Identity in the
mtl package.

(I haven't tried this code yet, so it may not really be syntactically
correct, but hopefully you get the idea.)

-Antoine


More information about the Haskell-Cafe mailing list