primes = sieve [2..]
where sieve (p:xs) =
p : sieve [x | x <- xs, x `mod` p /= 0]
As their first example of Haskell. It's a prime generator based on trial division. And not even trial division up to sqrt of the number you test, but all the way up.
I like the idea of a small snippet showing of the power of haskell, but there are many much better examples imho. Perhaps the website could just choose one at random at each load?
If the point is to show the power and elegance of the language, why would they complicate the expression with optimizations? The sqrt example is obvious to anyone who understands that line anyway.
What is the point of demonstrating the power an elegance with an example that will (hopefully!) never appear in any real world code? Better to show how the language is still powerful and elegant when taking into account the edge cases and common optimizations.
Writing elegant non-real-world code is much easier (in any language!) than writing real code.
2) Mark all numbers of the form i times n as composite, where i
is a positive integer. (We can do this by stepping over multiples of n. No multiplication needs to take place.
3) The next unmarked number is prime. Let it be n. Goto 2
Notice how this alogorithm does not use division. Division in modern CPUs is slow.
The algorithm presented uses the `mod` operator, which does division. This algorithm is equivalent to the Trial Division Algorithm. Instead of "deleting" multiples of n as composite, this algorithm tests all numbers greater than n for divisibilty by n, and then only does it "delete" the number.
primes = sieve [2..] where sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
As their first example of Haskell. It's a prime generator based on trial division. And not even trial division up to sqrt of the number you test, but all the way up.
I like the idea of a small snippet showing of the power of haskell, but there are many much better examples imho. Perhaps the website could just choose one at random at each load?