[Haskell-beginners] What is the functional way of implementing a function that takes a long time to execute?

Ertugrul Söylemez es at ertes.de
Wed Apr 24 06:12:59 CEST 2013


"Costello, Roger L." <costello at mitre.org> wrote:

> Thank you for your detailed explanation. I am not sure that I
> understand all of its subtleties.
>
> I am wondering if you would be willing to provide us with a simple,
> complete example that we can try out on our own machines?

I have written the Lucas-Lehmer primality test in various variants in
the this paste:

    <http://hpaste.org/86449>

The gist of it is that for a prime p you generate a sequence, pick a
particular element from it and compare it to zero.  If it is zero, then
2^p - 1 is a prime number.  In the paste the llSeq function is universal
to both the pure algorithm (llTest) that only computes the result as
well as the stats variant (llTestStats) that is an IO action and tells
you how many iterations are left to go.

For comparison I have also written a monolithic variant (llTestMono)
that doesn't use the stream processing style and shows how you would
implement the same algorithm in an imperative language.  This is
monolithic in that there is no flexibility here.

They are all about equal in speed.  Just compile and run with the
argument 10007:

    ./lucas-lehmer 10007

This will test whether the number 2^10007 - 1 is prime.  It should be
finished in less than a second and conclude with False.  The argument
11213 will conclude with True.


Greets,
Ertugrul

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130424/2ecea472/attachment-0001.pgp>


More information about the Beginners mailing list