Bonus prime-time!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module Analytics.Theory.Number.Continued.Continuations where

import Analytics.Theory.Number       -- http://lpaste.net/107480

{--

isPrimeC' isn't good enough! I mean, so you get ticked off (heh!)
with the progress, and that's good, and all, that you've done 57-ticks
worth of work, but so what? Eventually, you get bored waiting on the
factoring of, or the non-factoring of, some big-ole Mersenne prime, and
you ctrl-c out and what have you contributed to the search? Nothing!
Because how much progress did you make? Do you know? Writing isPrimeC',
I didn't know what progress I've made, and I didn't have a controlled
way to exit gracefully after NominalDiffTime t, where t was of magnitude:

"I'M BOOOOOORRRREDDDD HERE!"

So. That.

We need an exit-clause.

Or should I have just written that we need an 'escape'-clause.

DING-DING-DING! The lights are on! And the continuations are home.

Rewrite isPrimeC such that the continuation now has an escape, so that
part of the primality test includes in the StateT (oh, did I just
give it all away? I'm sorry!) a value of 'after X ticks, just give up
and return a 'no this number isn't prime, because it has factors'-value
or a 'well, this number is prime so far up to tested value v'-value.

I actually don't know how to write the declaration for a function like
this (well, I do, but my declaration may be far to restrictive or
specific for an approach that you have in mind ...

... like, for example, you may not even want to use StateT at all, and
may instead want to use ContT or something else entirely, and who says
you have to use monads for this, anyway? Am I right, or am I right!

So, I'll just write it as: --}

isPrimeC :: Monad r => Integer ->
            (Integer -> Integer -> r Bool) -> r Bool
isPrimeC p c = undefined

{-- for you for now with the heavy caveat that if a different function-type
works better for you, then please use it.  I know that for me I ended up
defining a series of types, including an escape function type:

type EscapeC = Bool -> ... something along the lines of r Bool

and my state was like this:

type StepState = ((Int, Integer), Int)  -- (step, testedUpto), maxsteps

but that was my (particular) approach, and not necessary to follow for this
problem's solution.

Have fun in bonus-land!

 --}