16 May 23:18
Re: Write Haskell as fast as C.
From: Martin Geisler <mg <at> daimi.au.dk>
Subject: Re: Write Haskell as fast as C.
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-05-16 21:18:49 GMT
Subject: Re: Write Haskell as fast as C.
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-05-16 21:18:49 GMT
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/.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe <at> haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
RSS Feed