[Haskell-cafe] Re: Write Haskell as fast as C.

Martin Geisler mg at daimi.au.dk
Fri May 16 17:18:49 EDT 2008

Andrew Coppin <andrewcoppin at btinternet.com> writes:

> Look closer: it's hardER to read.
>  mean xs = sum xs / fromIntegral (length xs)
>  mean = go 0 0 n
>    where
>      go s l x
>        | x > m = s / fromIntegra l
>        | otherwise = go (s+x) (l+1) (x+1
> One version makes it instantly clear, at a glance, what is happening.
> The other requires you to mentally walk round a look, imperative
> style, to figure out what's happening. It's not a *big* deal, but it's
> unfortunate.

I am new to Haskell and when I first saw the two versions side by side I
too was disappointed with the second version.

But after reading the great blog post by Don, I realized that the whole
problem comes from the fact that lists in Haskell are not like arrays or
vectors in other languages: you don't know how long they are before you
have found the end.

In other words: they behave like a linked lists -- lists that are
generated lazily on demand. Because they are generated on demand you
*cannot* really know the length beforehand, and thus you *must* traverse
the list to its end to count the length.

When the list is too big to fit in memory then it's clear that the code
*must* let go of the beginning to allow the garbage collector to do its
job. You wouldn't be able to work with a 7.5 GiB linked list otherwise.

Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multi-Party Computation) to Python. See: http://viff.dk/.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 188 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080516/c001efc9/attachment.bin

More information about the Haskell-Cafe mailing list