"takeWhile (<= (maxBound :: Word8)) primes" hangs

Today I found out how many primes there are that correspond to the boundaries Word8, but this very trivial task gave me an unexpected ... lack of result.

λ import Data.Numbers.Primes
λ import Data.Word
λ takeWhile (<= (maxBound :: Word8)) primes
[2,3,5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,53,55,59,61,71,73,
77,79,83,85,89,95,97,101,103,107,109,115,119,121,125,127,133,137,139,
145,149,151,157,161,163,167,173,175,179,185,187,191,197,199,203,205,
209,215,217,223,235^CInterrupted.

As you can see, this assessment not only does not end, but the numbers given are not all simple!

I continued to dance around the case:

λ maxBound :: Word8
255

λ takeWhile (<= 255) primes
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,
97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181
,191,193,197,199,211,223,227,229,233,239,241,251]

λ takeWhile (<= (maxBound :: Word8)) [1..]
[1,2,3, {- ..., -} 253,254,255]
-- This of course works, so I do not present the consecutive
-- natural numbers inbetween 3 and 253.

λ takeWhile (<= 255) $ take 255 primes
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,
97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181
,191,193,197,199,211,223,227,229,233,239,241,251]

λ takeWhile (<= (maxBound  :: Word8)) $ take 255 primes
[2,3,5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,53,55,59,61,71,73,
77,79,83,85,89,95,97,101,103,107,109,115,119,121,125,127,133,137,139,
145,149,151,157,161,163,167,173,175,179,185,187,191,197,199,203,205,
209,215,217,223,235^CInterrupted.

So, takeWhileit only works when we avoid either maxBound, or primes.

How do I interpret this data?


I use the latest stable stacksnapshot and this primes library .

+6
source share
2 answers

, takeWhile; primes:

primes , , , , . , Word8 , :

λ take 20 (primes :: [Word8])
[2,3,5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,53,55]

, (, (primes :: [Int]) fromIntegral) . , , .

+10

.

, (<= (maxBound :: Word8)) . p, :

p :: Word8 -> Bool
p x = x <= maxBound

, x ( ) Word8. , Word8 , Word8. !

, . .

takeWhile (<= fromIntegral (maxBound :: Word8)) (primes :: [Int])

x Int, fromIntegral 256::Word8 256::Int.

+5

All Articles