NavigationUser loginCMS by Drupal |
Laziness weirds programming 2This is a pretty clean definition of the Sieve of Eratosthenes in Haskell. Type it at the
Now, try " Here's the weird bit, though. Now try running the same expression again; you should be able to just back up a line and hit return. This completes instantly?! Apparently the evaluator keeps the computed portion of the list around in case you need it again. This "sharing" explains why you can get some surprising speedups on simple-looking codes. It also explains how you can easily and inadvertently leak a lot of memory. Laziness weirds programming. Update 2008/7/2: It's now not clear to me that anything in this post is right. First of all, the code given implements trial division, not Sieve of Eratosthenes. I'll produce that code later. Second of all, the code is gratuitously inefficient on several levels. This should be fixed. Finally, it's not clear that I am correctly understanding sharing. My colleagues and students seem confused also, so I'm not feeling so bad about this.
|
Post new comment